Message407233
On the subject of accuracy, there doesn't seem to be much in it on my mac laptop, and it looks as though pow(2.0, x) is giving correctly rounded results as often as (if not more often than) exp2(x).
Here's the log of a terminal session, after recompiling Python to add exp2. It shows the ulps error (tested against a high-precision Decimal computation, which we're treating as representing the "exact" result) for both exp2(x) and pow(2.0, x) when the two results differ, for a selection of randomly chosen x in the range(-1000.0, 1000.0). Columns in the output are:
x (in hex), x (in decimal), ulps error in exp2(x), ulps error in pow(2.0, x)
>>> from decimal import getcontext, Decimal
>>> from math import exp2, pow, ulp
>>> import random
>>> getcontext().prec = 200
>>> def exp2_error_ulps(x):
... libm = exp2(x)
... exactish = 2**Decimal(x)
... return float(Decimal(libm) - exactish) / ulp(libm)
...
>>> def pow2_error_ulps(x):
... libm = pow(2.0, x)
... exactish = 2**Decimal(x)
... return float(Decimal(libm) - exactish) / ulp(libm)
...
>>> for n in range(10000):
... x = random.uniform(-1000.0, 999.0) + random.random()
... if exp2(x) != pow(2.0, x):
... print(f"{x.hex():21} {x:22.17f} {exp2_error_ulps(x): .5f}, {pow2_error_ulps(x): .5f}")
...
0x1.e28f2ad3da122p+5 60.31990590581177969 0.50669, -0.49331
-0x1.929e790e1d293p+9 -805.23806930946227567 0.50082, -0.49918
-0x1.49803564f5b8ap+8 -329.50081473349621319 0.49736, -0.50264
-0x1.534cf08081f4bp+8 -339.30054476902722627 -0.50180, 0.49820
-0x1.b430821fb4ad2p+8 -436.18948553238908517 -0.49883, 0.50117
0x1.2c87a8431bd8fp+8 300.52991122655743084 -0.50376, 0.49624
0x1.3e476f9a09c8cp+7 159.13952332848964488 0.50062, -0.49938
0x1.cb8b9c61e7e89p+9 919.09070991347937252 0.49743, -0.50257
0x1.ab86ed0e6c7f6p+9 855.05410938546879152 0.49742, -0.50258
0x1.97bc9af3cbf85p+9 815.47347876986952997 -0.50076, 0.49924
-0x1.b5434441ba11bp+8 -437.26276026528074681 -0.50062, 0.49938
-0x1.0ead35218910ep+9 -541.35318392937347198 0.50192, -0.49808
-0x1.dbae0b861b89cp+9 -951.35972668022759535 0.50601, -0.49399
0x1.522f005d2dcc4p+6 84.54589982597377684 -0.50704, 0.49296
0x1.398ff48d53ee1p+9 627.12465063665524667 -0.50102, 0.49898
-0x1.381307fbd89f5p+5 -39.00929257159069863 -0.50526, 0.49474
0x1.9dc4c85f7c53ap+9 827.53736489840161994 -0.50444, 0.49556
0x1.b357f6012d3c2p+9 870.68719496449216422 -0.50403, 0.49597
-0x1.a6446703677bbp+9 -844.53439371636284250 0.50072, -0.49928
0x1.e3dd54b28998bp+7 241.93228681497234334 0.49897, -0.50103
0x1.b4f77f18a233ep+8 436.96678308448815642 0.49593, -0.50407
-0x1.578c4ce7a7c1bp+3 -10.73587651486564276 -0.50505, 0.49495
0x1.25a9540e1ee65p+5 36.70767985374258302 0.49867, -0.50133
-0x1.6e220f7db7668p+8 -366.13304887511776542 -0.49904, 0.50096
-0x1.94214ed3e5264p+9 -808.26021813095985635 0.50420, -0.49580
0x1.9dcc3d281da18p+5 51.72472602215219695 -0.50423, 0.49577
-0x1.3ba66909e6a40p+7 -157.82502013149678532 -0.50077, 0.49923
-0x1.9eac2c52a1b47p+9 -829.34510262389892432 -0.50540, 0.49460 |
|
Date |
User |
Action |
Args |
2021-11-28 21:33:10 | mark.dickinson | set | recipients:
+ mark.dickinson, tim.peters, rhettinger, serhiy.storchaka, Gideon |
2021-11-28 21:33:10 | mark.dickinson | set | messageid: <1638135190.3.0.180235147182.issue45917@roundup.psfhosted.org> |
2021-11-28 21:33:10 | mark.dickinson | link | issue45917 messages |
2021-11-28 21:33:09 | mark.dickinson | create | |
|