in reply to Re^2: Math::Base - arithmetics with baseX integers (OP updated)
in thread Math::Base - arithmetics with baseX integers (updated)
$num = int $num;
$num = (~abs($num))+1 if $num < 0;
You can get the same effect with $num |= 0; ...but... why? Why would you want two's complement behavior in other bases?
Truncating at $n bits is mathematically equivalent to:
$num %= 2 ** $n;That's only meaningful for base-2. You can truncate at $n base-$b digits using this:
$num %= $b ** $n;So -1 becomes 999999 in base-10 or 666666 in base-7. If you want, you can pick a big number of digits that still fits in a double-precision float like this:
$num %= $base ** int(36.73/log($base));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Math::Base - arithmetics with baseX integers (OP updated)
by shmem (Chancellor) on Aug 23, 2017 at 19:45 UTC | |
by no_slogan (Deacon) on Aug 24, 2017 at 16:09 UTC | |
|
Re^4: Math::Base - arithmetics with baseX integers (OP updated)
by BrowserUk (Patriarch) on Aug 24, 2017 at 00:54 UTC | |
by no_slogan (Deacon) on Aug 24, 2017 at 03:01 UTC | |
by BrowserUk (Patriarch) on Aug 24, 2017 at 12:35 UTC | |
by no_slogan (Deacon) on Aug 24, 2017 at 14:21 UTC |