+ 1

Binary to decimal convertion error

I want to solve function >> y=-2^x But this metod down here shows values that are very strange for me :D I know that that its caused by that python in binary nubers cant save nubers like 9.9 9.8 etc... Do you know any method how to have clean results? a=-10.0 for i in range (201): print("-2^",a,"=",(-2)**a) a+=0.1

2nd Nov 2016, 12:11 AM
dede 64 (a008x8)
dede 64 (a008x8) - avatar
2 Answers
+ 1
So...this is all new to me but I've been picking up on the abundance of hints here related to float storage. And that's a disclaimer to verify; yourself. Floats aren't precise. Decimals are, but they're immutable. It looks like this works: >>> from decimal import * >>> a = Decimal('-10.0') >>> a += Decimal('0.1') >>> a Decimal('-9.9') >>> 2**-9.9 0.0010466537720080985 >>> 2**a Decimal('0.001046653772008098793176763989')
2nd Nov 2016, 12:50 AM
Kirk Schafer
Kirk Schafer - avatar
0
Now i know why it output so terrible outputs, because you cant power minus numbers with float
2nd Nov 2016, 8:51 AM
dede 64 (a008x8)
dede 64 (a008x8) - avatar