Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #124609 > unrolled thread
| Started by | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| First post | 2026-08-11 03:01 -0500 |
| Last post | 2026-08-18 11:32 +0200 |
| Articles | 20 on this page of 132 — 15 participants |
Back to article view | Back to comp.lang.c++
why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-11 03:01 -0500
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 13:11 +0200
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 12:45 +0100
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 14:34 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 14:55 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 15:19 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 15:27 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 16:08 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 16:18 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 17:11 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 17:20 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 17:32 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 17:39 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 18:33 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 18:39 +0200
Re: why is there not a ipow version of pow? Michael S <already5chosen@yahoo.com> - 2026-08-11 23:31 +0300
Re: why is there not a ipow version of pow? James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-08-11 11:46 -0400
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 18:35 +0200
Re: why is there not a ipow version of pow? antispam@fricas.org (Waldek Hebisch) - 2026-08-11 22:53 +0000
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-12 10:08 +0200
Re: why is there not a ipow version of pow? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-12 03:58 -0700
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-12 13:31 +0200
Re: why is there not a ipow version of pow? Michael S <already5chosen@yahoo.com> - 2026-08-12 22:43 +0300
Re: why is there not a ipow version of pow? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-12 13:44 -0700
Re: why is there not a ipow version of pow? Michael S <already5chosen@yahoo.com> - 2026-08-13 00:24 +0300
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-12 16:49 -0500
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-13 08:38 +0200
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-13 18:59 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-13 18:51 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 00:58 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 12:08 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 15:48 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 13:51 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 16:40 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 19:24 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 22:51 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 22:33 -0700
Re: why is there not a ipow version of pow? Ross Finlayson <ross.a.finlayson@gmail.com> - 2026-08-15 10:40 -0700
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-15 11:41 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-17 14:48 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-16 14:58 -0700
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-14 08:57 +0200
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 12:46 -0700
Re: why is there not a ipow version of pow? James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-08-13 12:59 -0400
Re: why is there not a ipow version of pow? antispam@fricas.org (Waldek Hebisch) - 2026-08-12 20:23 +0000
Re: why is there not a ipow version of pow? scott@slp53.sl.home (Scott Lurndal) - 2026-08-11 18:19 +0000
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 20:21 +0200
Re: why is there not a ipow version of pow? scott@slp53.sl.home (Scott Lurndal) - 2026-08-11 21:10 +0000
Re: why is there not a ipow version of pow? Michael S <already5chosen@yahoo.com> - 2026-08-12 00:42 +0300
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 06:41 +0200
Re: why is there not a ipow version of pow? scott@slp53.sl.home (Scott Lurndal) - 2026-08-12 14:19 +0000
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 16:56 +0200
Re: why is there not a ipow version of pow? scott@slp53.sl.home (Scott Lurndal) - 2026-08-12 15:32 +0000
Re: why is there not a ipow version of pow? Michael S <already5chosen@yahoo.com> - 2026-08-12 22:05 +0300
Re: why is there not a ipow version of pow? Paul <nospam@needed.invalid> - 2026-08-12 19:09 -0400
Re: why is there not a ipow version of pow? Michael S <already5chosen@yahoo.com> - 2026-08-11 21:58 +0300
Re: why is there not a ipow version of pow? Ross Finlayson <ross.a.finlayson@gmail.com> - 2026-08-11 21:44 -0700
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 15:45 +0100
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 16:56 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 17:23 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 17:24 +0200
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 16:23 +0100
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 17:26 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 17:54 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 18:40 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 08:49 +0200
Re: why is there not a ipow version of pow? Paul <nospam@needed.invalid> - 2026-08-12 09:11 -0400
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 16:05 +0200
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-12 12:40 -0700
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-13 15:46 +0200
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-13 14:28 -0700
Re: why is there not a ipow version of pow? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-11 16:20 -0700
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 18:43 +0100
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 19:56 +0200
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 20:41 +0100
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 06:42 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-12 10:23 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 20:24 +0200
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 20:27 +0100
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 06:45 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 17:16 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 15:17 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 15:28 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 16:43 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-11 16:47 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-11 17:24 +0200
Re: why is there not a ipow version of pow? Paul <nospam@needed.invalid> - 2026-08-11 09:36 -0400
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-11 16:42 -0500
Re: why is there not a ipow version of pow? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-08-11 19:15 +0200
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-11 16:41 -0500
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-11 16:49 -0500
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-11 23:28 +0100
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-11 17:51 -0500
Re: why is there not a ipow version of pow? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-11 15:33 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-11 17:49 -0500
Re: why is there not a ipow version of pow? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-08-11 16:19 -0700
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-12 00:32 -0500
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 14:08 +0200
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-12 18:40 +0200
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-14 02:50 +0000
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-12 04:26 +0000
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-12 01:37 -0500
Re: why is there not a ipow version of pow? Paul <nospam@needed.invalid> - 2026-08-13 00:12 -0400
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-14 11:22 +0800
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 01:00 -0500
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-14 14:47 +0800
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 13:54 -0500
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-16 22:49 +0000
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-17 14:56 -0500
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-17 23:53 +0000
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-17 19:59 -0500
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-18 02:47 +0000
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-17 15:17 -0500
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 12:03 -0700
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-15 03:51 +0800
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 12:52 -0700
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-15 04:01 +0800
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 13:27 -0700
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 13:29 -0700
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-15 04:49 +0800
Re: why is there not a ipow version of pow? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-08-14 20:32 -0700
Re: why is there not a ipow version of pow? bart <bc@freeuk.com> - 2026-08-12 11:28 +0100
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-12 23:52 +0000
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-13 17:19 +0800
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-13 19:07 -0500
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-14 10:58 +0800
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-14 01:14 -0500
Re: why is there not a ipow version of pow? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-17 06:40 +0000
Re: why is there not a ipow version of pow? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-17 21:40 +0800
Re: why is there not a ipow version of pow? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-08-18 00:42 -0500
Re: why is there not a ipow version of pow? Bonita Montero <Bonita.Montero@gmail.com> - 2026-08-18 09:34 +0200
Re: why is there not a ipow version of pow? David Brown <david.brown@hesbynett.no> - 2026-08-18 11:32 +0200
Page 4 of 7 — ← Prev page 1 2 3 [4] 5 6 7 Next page →
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-11 17:24 +0200 |
| Message-ID | <115ferj$3r8j$1@raubtier-asyl.eternal-september.org> |
| In reply to | #124628 |
Am 11.08.2026 um 17:23 schrieb David Brown: > If you are writing a real integer power function with speed in mind, > don't do that. ... If I want to compare the speed against a fp-pow() that's the right way.
[toc] | [prev] | [next] | [standalone]
| From | bart <bc@freeuk.com> |
|---|---|
| Date | 2026-08-11 16:23 +0100 |
| Message-ID | <115fepj$3khp$1@dont-email.me> |
| In reply to | #124624 |
On 11/08/2026 15:56, Bonita Montero wrote:
> This is the integer pow() code so far I wrote in C++:
>
> template<typename Int>
> constexpr optional<Int> ipow( Int b, Int e )
> {
> constexpr bool Sgn = is_signed_v<Int>;
> using uint = make_unsigned_t<Int>;
> if( !b )
> return !e;
> uint ub, ue;
> if constexpr( Sgn )
> if( e >= 0 )
> {
> ub = abs( b );
> ue = abs( e );
> }
> else
> return abs( b ) == 1;
> else
> ub = b, ue = e;
> uint result = 1, msk = 1, sq = ub;
> while( ue )
> {
> if( (ue & msk) )
> {
> uint next = result * sq;
> if( next / sq != result )
> return nullopt;
> result = next;
> ue &= ~msk;
> }
> msk <<= 1;
> if( sq * sq / sq != sq )
> return nullopt;
> sq *= sq;
> }
> if constexpr( Sgn )
> if( bool neg = b < 0; neg && (e & 1) )
> if( result <= (uint)numeric_limits<Int>::min() )
> result = -(Int)result;
> else
> return nullopt;
> return result;
>
> }
Can you package this into something I can all as ipow(a, b)?
As it is this doesn't compile by itself.
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-11 17:26 +0200 |
| Message-ID | <115feup$3r8j$2@raubtier-asyl.eternal-september.org> |
| In reply to | #124629 |
Am 11.08.2026 um 17:23 schrieb bart:
> On 11/08/2026 15:56, Bonita Montero wrote:
>> This is the integer pow() code so far I wrote in C++:
>>
>> template<typename Int>
>> constexpr optional<Int> ipow( Int b, Int e )
>> {
>> constexpr bool Sgn = is_signed_v<Int>;
>> using uint = make_unsigned_t<Int>;
>> if( !b )
>> return !e;
>> uint ub, ue;
>> if constexpr( Sgn )
>> if( e >= 0 )
>> {
>> ub = abs( b );
>> ue = abs( e );
>> }
>> else
>> return abs( b ) == 1;
>> else
>> ub = b, ue = e;
>> uint result = 1, msk = 1, sq = ub;
>> while( ue )
>> {
>> if( (ue & msk) )
>> {
>> uint next = result * sq;
>> if( next / sq != result )
>> return nullopt;
>> result = next;
>> ue &= ~msk;
>> }
>> msk <<= 1;
>> if( sq * sq / sq != sq )
>> return nullopt;
>> sq *= sq;
>> }
>> if constexpr( Sgn )
>> if( bool neg = b < 0; neg && (e & 1) )
>> if( result <= (uint)numeric_limits<Int>::min() )
>> result = -(Int)result;
>> else
>> return nullopt;
>> return result;
>>
>> }
>
>
> Can you package this into something I can all as ipow(a, b)?
You need <optional>, type_traits>, <cmath>, <random>.
> As it is this doesn't compile by itself.
Don't you program C ?
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-11 17:54 +0200 |
| Message-ID | <115fgjg$4evm$1@raubtier-asyl.eternal-september.org> |
| In reply to | #124629 |
I did handle two additional corner cases, but I still don't know
if I'm 100% correct.
template<typename Int>
constexpr optional<Int> ipow( Int b, Int e )
{
constexpr bool Sgn = is_signed_v<Int>;
using uint = make_unsigned_t<Int>;
if( !b )
return !e;
uint ub, ue;
if constexpr( Sgn )
if( e >= 0 )
{
ub = abs( b );
ue = abs( e );
}
else
return abs( b ) == 1 ? b : 0;
else
ub = b, ue = e;
uint result = 1, msk = 1, sq = ub;
while( ue )
{
if( (ue & msk) )
{
uint next = result * sq;
if( next / sq != result )
return nullopt;
result = next;
ue &= ~msk;
}
msk <<= 1;
if( (uint)(sq * sq) / sq != sq )
return nullopt;
sq *= sq;
}
constexpr uint Max = numeric_limits<Int>::min();
if constexpr( Sgn )
if( b < 0 )
if( (e & 1) )
if( result <= Max )
result = -(Int)result;
else
return nullopt;
else
if( result >= Max )
return nullopt;
else
if( result >= Max )
return nullopt;
return result;
}
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-11 18:40 +0200 |
| Message-ID | <115fjae$5cs5$2@raubtier-asyl.eternal-september.org> |
| In reply to | #124629 |
This is further improved with some bit-scanning to find the
next set bit in the exponent beginning from the lowest bit on.
This should be somewhat faster.
template<typename Int>
constexpr optional<Int> ipow( Int b, Int e )
{
constexpr bool Sgn = is_signed_v<Int>;
using uint = make_unsigned_t<Int>;
if( !b )
return !e;
uint ub, ue;
if constexpr( Sgn )
if( e >= 0 )
{
ub = abs( b );
ue = abs( e );
}
else
return abs( b ) == 1 ? b : 0;
else
ub = b, ue = e;
uint result = 1, msk = 1, sq = ub;
for( int xskip = 0; ue; xskip = 1 )
{
int skip = countr_zero( ue );
for( int s = skip + xskip; s; sq *= sq, --s )
if( (uint)(sq * sq) / sq != sq )
return nullopt;
if( (uint)(result * sq) / sq != result )
return nullopt;
result *= sq;
ue >>= skip;
ue >>= 1;
}
constexpr uint UMax = numeric_limits<Int>::min();
if constexpr( Sgn )
if( b < 0 )
if( (e & 1) )
if( result <= UMax )
result = -(Int)result;
else
return nullopt;
else
if( result >= UMax )
return nullopt;
else
if( result >= UMax )
return nullopt;
return result;
}
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-12 08:49 +0200 |
| Message-ID | <115h526$jke4$1@raubtier-asyl.eternal-september.org> |
| In reply to | #124640 |
There was a very little mistake with my last code: If the exponent is negative (immediate return), abs( base ) == 1 and the exponent is odd, the return is not 1 vs. 0 but -1 vs 0. And there are two *theoretical* UBs I cover. If either base or exponent have the maximum negative values negating them is UB. So now I do a 0u - (uint)value. But that's rather aesthetics on today's platforms. Claude and GPT don't complain about my code now. Didn't think that this code would be so tricky at last. And the major issue with my code is: no one needs a pow for integers, even my pow() is faster for valid values than a fp-pow().
[toc] | [prev] | [next] | [standalone]
| From | Paul <nospam@needed.invalid> |
|---|---|
| Date | 2026-08-12 09:11 -0400 |
| Message-ID | <115hrea$r4pu$1@dont-email.me> |
| In reply to | #124670 |
On Wed, 8/12/2026 2:49 AM, Bonita Montero wrote:
> There was a very little mistake with my last code: If the exponent is
> negative (immediate return), abs( base ) == 1 and the exponent is odd,
> the return is not 1 vs. 0 but -1 vs 0.
> And there are two *theoretical* UBs I cover. If either base or exponent
> have the maximum negative values negating them is UB. So now I do a
> 0u - (uint)value. But that's rather aesthetics on today's platforms.
> Claude and GPT don't complain about my code now.
> Didn't think that this code would be so tricky at last. And the major
> issue with my code is: no one needs a pow for integers, even my pow()
> is faster for valid values than a fp-pow().
And this highlights the issue with crafting your own.
https://gmplib.org/manual/Integer-Functions
https://gmplib.org/manual/Integer-Exponentiation
Function: void mpz_pow_ui (mpz_t rop, const mpz_t base, unsigned long int exp)
Function: void mpz_ui_pow_ui (mpz_t rop, unsigned long int base, unsigned long int exp)
Maybe one of those two is actually an ipow() :-)
And since this function is so ugly (the special cases may not
make any sense to you, like who goes around doing ipow(0,0)),
you really need a test bench for it. For some softwares,
the test bench has more intellectual property associated with
it, than any generated code for it. Getting routines like this
right, is easier when what the code is doing, matches your
grade school knowledge of what the answer should be.
I would not trust an LLM-AI to do an off-the-cuff analysis
of the code for this one. Claude may have a slight edge over
the competition, but LLM-AI really do make mistakes, and it
isn't pretty when they do.
Paul
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-12 16:05 +0200 |
| Message-ID | <115huih$s5sn$1@raubtier-asyl.eternal-september.org> |
| In reply to | #124677 |
Am 12.08.2026 um 15:11 schrieb Paul: > I would not trust an LLM-AI to do an off-the-cuff analysis > of the code for this one. Claude may have a slight edge over > the competition, but LLM-AI really do make mistakes, and it > isn't pretty when they do. If Claude sees 95% of all issues that's useful.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2026-08-12 12:40 -0700 |
| Message-ID | <115ii6v$131cb$1@dont-email.me> |
| In reply to | #124678 |
On 8/12/2026 7:05 AM, Bonita Montero wrote: > Am 12.08.2026 um 15:11 schrieb Paul: > > > I would not trust an LLM-AI to do an off-the-cuff analysis >> of the code for this one. Claude may have a slight edge over >> the competition, but LLM-AI really do make mistakes, and it >> isn't pretty when they do. > > If Claude sees 95% of all issues that's useful. > Why not use your own mind, for your own work?
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-13 15:46 +0200 |
| Message-ID | <115khsd$1mb6o$2@raubtier-asyl.eternal-september.org> |
| In reply to | #124685 |
Am 12.08.2026 um 21:40 schrieb Chris M. Thomasson: > On 8/12/2026 7:05 AM, Bonita Montero wrote: >> Am 12.08.2026 um 15:11 schrieb Paul: >> >> > I would not trust an LLM-AI to do an off-the-cuff analysis >>> of the code for this one. Claude may have a slight edge over >>> the competition, but LLM-AI really do make mistakes, and it >>> isn't pretty when they do. >> >> If Claude sees 95% of all issues that's useful. >> > > Why not use your own mind, for your own work? Why are a lot of developers working in teams and not only on their own ?
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2026-08-13 14:28 -0700 |
| Message-ID | <115lct2$20otj$1@dont-email.me> |
| In reply to | #124697 |
On 8/13/2026 6:46 AM, Bonita Montero wrote: > Am 12.08.2026 um 21:40 schrieb Chris M. Thomasson: >> On 8/12/2026 7:05 AM, Bonita Montero wrote: >>> Am 12.08.2026 um 15:11 schrieb Paul: >>> >>> > I would not trust an LLM-AI to do an off-the-cuff analysis >>>> of the code for this one. Claude may have a slight edge over >>>> the competition, but LLM-AI really do make mistakes, and it >>>> isn't pretty when they do. >>> >>> If Claude sees 95% of all issues that's useful. >>> >> >> Why not use your own mind, for your own work? > > Why are a lot of developers working in teams and not only on their own ? Kind of sounds like you are allowing the AI to be you. Sigh.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2026-08-11 16:20 -0700 |
| Message-ID | <115ganv$c4r1$3@kst.eternal-september.org> |
| In reply to | #124629 |
bart <bc@freeuk.com> writes:
> On 11/08/2026 15:56, Bonita Montero wrote:
>> This is the integer pow() code so far I wrote in C++:
[snip]
>
> Can you package this into something I can all as ipow(a, b)?
>
> As it is this doesn't compile by itself.
Not by itself, no. I was able to get it to compile by adding a
few lines at the top. I won't go into details in a thread that's
inappropriately cross-posted to comp.lang.c.
Ask in comp.lang.c++ without the crosspost to comp.lang.c, and I'll
be glad to explain if nobody else does so first.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | bart <bc@freeuk.com> |
|---|---|
| Date | 2026-08-11 18:43 +0100 |
| Message-ID | <115fmvu$6h03$1@dont-email.me> |
| In reply to | #124624 |
On 11/08/2026 15:56, Bonita Montero wrote: > I didn't test all corner cases, but for values which don't overflow the > code should be corrent. The crucial case about the performance here is > that I need a division to check for overflows; in these cases you get > a nullopt. With fp-values you get inf and that's less expensive. > Why do you need all that? You basically just want ipow(a, 4) to return a*a*a*a. If worried about overflow, then use floating point pow().
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-11 19:56 +0200 |
| Message-ID | <115fnos$71sp$1@raubtier-asyl.eternal-september.org> |
| In reply to | #124642 |
Am 11.08.2026 um 19:43 schrieb bart: > Why do you need all that? You basically just want ipow(a, 4) to return > a*a*a*a. To have variable exponents. > If worried about overflow, then use floating point pow(). I did this to compare the performance, but I didn't run the numbers on that. I mentioned that the overflow detection needs a division, thereby making the code not competitive to a fp-version.
[toc] | [prev] | [next] | [standalone]
| From | bart <bc@freeuk.com> |
|---|---|
| Date | 2026-08-11 20:41 +0100 |
| Message-ID | <115ftt7$8db6$2@dont-email.me> |
| In reply to | #124643 |
On 11/08/2026 18:56, Bonita Montero wrote: > Am 11.08.2026 um 19:43 schrieb bart: > >> Why do you need all that? You basically just want ipow(a, 4) to return >> a*a*a*a. > > To have variable exponents. You mean ipow(a, b) where b is known at runtime? Then that's exactly what I mean. This should be the equivalant of a*a*a...*a with b-1 multiplications, but done more efficiently. For example my function can calculate ipow(1, 1000000) 10,000 times faster than a version that simply does 1000000 or so multiplications. >> If worried about overflow, then use floating point pow(). > > I did this to compare the performance, but I didn't run the numbers on > that. I mentioned that the overflow detection needs a division, thereby > making the code not competitive to a fp-version. > People don't care about integer overflow in general, why should they here? However if large-magnitude results (greater than 2**63) are likely, then they should go with floats and use pow, at a loss of some precision. Or use the same algorithm with a bigint library as I also do.
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-12 06:42 +0200 |
| Message-ID | <115gtk2$hnni$2@raubtier-asyl.eternal-september.org> |
| In reply to | #124649 |
Am 11.08.2026 um 21:41 schrieb bart: > People don't care about integer overflow in general, why should they here? Yes, because it happens almost never. But when it happens you handle it. And with an integer pow() it's rather likely.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2026-08-12 10:23 +0200 |
| Message-ID | <115hahp$kpdk$2@dont-email.me> |
| In reply to | #124649 |
On 11/08/2026 21:41, bart wrote: > On 11/08/2026 18:56, Bonita Montero wrote: >> Am 11.08.2026 um 19:43 schrieb bart: >> >>> Why do you need all that? You basically just want ipow(a, 4) to >>> return a*a*a*a. >> >> To have variable exponents. > > You mean ipow(a, b) where b is known at runtime? > > Then that's exactly what I mean. This should be the equivalant of > a*a*a...*a with b-1 multiplications, but done more efficiently. > > For example my function can calculate ipow(1, 1000000) 10,000 times > faster than a version that simply does 1000000 or so multiplications. > > >>> If worried about overflow, then use floating point pow(). >> >> I did this to compare the performance, but I didn't run the numbers on >> that. I mentioned that the overflow detection needs a division, thereby >> making the code not competitive to a fp-version. >> > > People don't care about integer overflow in general, why should they here? Most people /do/ care about integer overflows - they aim to use types and expressions that don't overflow. If you assume that the person calling your "ipow" function only does so with sensible values, then the implementation of "ipow" doesn't have to consider overflow and can be simpler and faster (like your code, or my code). This is not because no one cares about integer overflow, it's about putting the responsibility in the place where the potential bug actually exists and can therefore be fixed. Of course it is also possible to write an integer power function that saturates on overflow, or that has some error indicator, but that's a different function specification. Bonita is apparently trying to code for such a function. > > However if large-magnitude results (greater than 2**63) are likely, then > they should go with floats and use pow, at a loss of some precision. > Yes. Or it might be more useful to have a half-way function: double power_by_int(double a, int b); (Here "b" is allowed to be negative.) That would use the same multiplication algorithm as we have shown previously. > Or use the same algorithm with a bigint library as I also do. > For big values of "b", the most common usage of "a ** b" is to calculate "(a ** b) % c". And clearly you do that as a single function, not by doing the power operation then the modulo operation. But once you have your bigint support for multiplication, adding a "power" function like the one you wrote is going to be quite simple even if it is never used for large values of "b".
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-11 20:24 +0200 |
| Message-ID | <115fpc8$7itg$2@raubtier-asyl.eternal-september.org> |
| In reply to | #124642 |
Now I did run the numbers on that. My integer code is about four
times faster with g++ and clang++/clang-cl for all cases where
there's no overflow on ipow() *and* pow(). I didn't expect that.
#include <iostream>
#include <optional>
#include <type_traits>
#include <cmath>
#include <random>
#include <bit>
#include <chrono>
using namespace std;
using namespace chrono;
template<typename Int>
constexpr optional<Int> ipow( Int b, Int e );
int main()
{
constexpr int64_t Base = -3;
constexpr size_t Rounds = 10'000'000;
int maxExp = 0;
for( ; ipow( Base, (int64_t)maxExp ); ++maxExp );
int64_t joined = 0;
auto tm = [&]( const char *what, auto fn )
{
time_point start = steady_clock::now();
for( size_t r = Rounds; r; --r )
for( int exp = 0; exp <= maxExp; ++exp )
joined ^= fn( exp );
duration dur = duration_cast<nanoseconds>( steady_clock::now() - start );
double ns = (double)dur.count() / ((double)Rounds * (double)(maxExp + 1));
cout << what << ns << endl;
};
tm( "integer: ", []( int exp ) { return *ipow( Base, (int64_t)exp ); } );
tm( "double: ", []( int exp ) { return bit_cast<int64_t>( pow(
(double)Base, exp ) ); } );
return (int)joined;
}
template<typename Int>
constexpr optional<Int> ipow( Int b, Int e )
{
constexpr bool Sgn = is_signed_v<Int>;
using uint = make_unsigned_t<Int>;
if( !b )
return !e;
uint ub, ue;
if constexpr( Sgn )
if( e >= 0 )
{
ub = abs( b );
ue = abs( e );
}
else
return abs( b ) == 1 ? b : 0;
else
ub = b, ue = e;
uint result = 1, sq = ub;
for( int xskip = 0; ue; xskip = 1 )
{
int skip = countr_zero( ue );
for( int s = skip + xskip; s; sq *= sq, --s )
if( (uint)(sq * sq) / sq != sq )
return nullopt;
if( (uint)(result * sq) / sq != result )
return nullopt;
result *= sq;
ue >>= skip;
ue >>= 1;
}
constexpr uint UMax = numeric_limits<Int>::min();
if constexpr( Sgn )
if( b < 0 && (e & 1) )
if( result <= UMax )
result = -(Int)result;
else
return nullopt;
else
if( result >= UMax )
return nullopt;
return result;
}
[toc] | [prev] | [next] | [standalone]
| From | bart <bc@freeuk.com> |
|---|---|
| Date | 2026-08-11 20:27 +0100 |
| Message-ID | <115ft30$8db6$1@dont-email.me> |
| In reply to | #124646 |
On 11/08/2026 19:24, Bonita Montero wrote:
> Now I did run the numbers on that. My integer code is about four
> times faster with g++ and clang++/clang-cl for all cases where
> there's no overflow on ipow() *and* pow(). I didn't expect that.
When I tried to compile this, it said:
C:/tdm/include/c++/16.1.0/optional:1256: constexpr _Tp&&
std::optional<_Tp>::operator*() && [with _T
p = long long int]: Assertion 'this->_M_is_engaged()' failed.
I don't know enough C++ to fix it, but, got rid of enough stuff to get
it to compile, while still doing the task.
The version below, compiled with g++-O2, completed in 0.5 seconds.
My C version, with the same driver code and compiled with gcc-O2,
completed in 0.2 seconds (0.25 if ipow was in a separate file; normally
ipow() would be in a library).
If doing ipow(2, 60) instead, yours took 0.9s compared with my 0.4(0.5)
seconds.
---------------------------------
#include <iostream>
#include <optional>
#include <type_traits>
#include <cmath>
#include <random>
#include <bit>
#include <chrono>
using namespace std;
using namespace chrono;
typedef long long int Int;
Int ipow( Int b, Int e )
{
constexpr bool Sgn = is_signed_v<Int>;
using uint = make_unsigned_t<Int>;
if( !b )
return !e;
uint ub, ue;
if constexpr( Sgn )
if( e >= 0 )
{
ub = abs( b );
ue = abs( e );
}
else
return abs( b ) == 1 ? b : 0;
else
ub = b, ue = e;
uint result = 1, sq = ub;
for( int xskip = 0; ue; xskip = 1 )
{
int skip = countr_zero( ue );
for( int s = skip + xskip; s; sq *= sq, --s )
if( (uint)(sq * sq) / sq != sq )
return 0;
if( (uint)(result * sq) / sq != result )
return 0;
result *= sq;
ue >>= skip;
ue >>= 1;
}
return result;
}
int main() {
volatile Int a=4,b=3,c;
for (int i=0; i<100000000; ++i)
c=ipow(a, b);
printf("%d\n", c);
}
------------------
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2026-08-12 06:45 +0200 |
| Message-ID | <115gtp5$hnni$3@raubtier-asyl.eternal-september.org> |
| In reply to | #124648 |
Am 11.08.2026 um 21:27 schrieb bart: > When I tried to compile this, it said: > C:/tdm/include/c++/16.1.0/optional:1256: constexpr _Tp&& > std::optional<_Tp>::operator*() && [with _T > p = long long int]: Assertion 'this->_M_is_engaged()' failed. For me it compiles with g++-14 and clang++-20 and C++23 (-std:c++20).
[toc] | [prev] | [next] | [standalone]
Page 4 of 7 — ← Prev page 1 2 3 [4] 5 6 7 Next page →
Back to top | Article view | comp.lang.c++
csiph-web