0

Regarding float math. Would it be possible in 2017 to reengineer computers/standards so that you have

-- int (x)

-- decimal (fixed positional without trails x.x or x.xxn) ((in the mean time I have created a hack in the answer below))

-- float (here trails are allowed as in the esoteric nature of the float x.n?)

Below is the original text I wrote and a video that addresses floating point CppCon 2015:

I am programming an iterator that will loop from 0.0 to 3.0.

i = 0.0
while i < 3:
  do something with i
  i += 0.2

But when I do the += 0.2 the resulting numbers are not the expected 0.2 then 0.4 but

0.19999990 and then 0.3999999999

If i do round it does not help.

If instead I do

from decimal import Decimal, getcontext 

the numbers gets even worse.

Can Python somehow be mad(e) to interpret it correctly that 0.2 increments means just that and not much longer decimals? I mean is there something between int and float that will do the trick, where decimal does not. Or I were taught wrong in school that 0.1 really means 0.1000009?

Drill Bit
  • 57
  • 8
  • Read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). Hard read, but good read. And you'll never question floating-point arithmetic again. – cadaniluk Aug 01 '17 at 18:12
  • 3
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Ignacio Vazquez-Abrams Aug 01 '17 at 18:25
  • I could change the question because it seems like a duplicate. But I am not satisfied computers do not have definite 0.2-like decimal type but only superficial precision cut off. I will read the Oracle appendix and see if I will be convinced the state of affairs are satisfactory. – Drill Bit Aug 01 '17 at 18:43
  • Consider a float of finite width cannot represent every number written as text. What should code do with values that are not exactly representable as _float_? Round to the closest representable float? – chux - Reinstate Monica Aug 01 '17 at 20:16
  • 2
    How does Decimal make things worse? It should do exactly what you want. – Rudy Velthuis Aug 01 '17 at 23:00

1 Answers1

-1

EDIT: The "solution" below seemed like a solution until it was tested several times. It is as bad as normal floats.

OLD: I have done a hack.....it "works" and would be able to scale down as far as a programmer is willing to suffer nested while-loops. I call it the nested integer decimal method or the flying donkey.

a1 = 0
a2 = 0
b = str(a1)+'.'+str(a2)
float(b)

an example:

while a1 < 3:
  b2 = 0
  while a2 < 10:
    b = str(a1)+'.'+str(a2)
    do stuff including float(b)
    a2 += 1
  a1 += 1
Drill Bit
  • 57
  • 8
  • What exactly does this gain you over `for i in range(30): print(i/10.0)`? – Sneftel Aug 02 '17 at 08:25
  • It does not gain me anything. It was a response to the float problem. The range solution is much better and I will use that instead. Flying donkeys are mythical creatures. – Drill Bit Aug 02 '17 at 08:53
  • @Sneftel one problem is that I cannot start at an arbitrary point, to my knowledge. If i want to start at 1.23456 and do the range it starts at 1 and not 1.23456. If I do a float it will be 1.23456000021325 or something. My hacked solution could with enough effort start at an arbitrary postion and count up from there. Only giving me what I want where int and float cannot go it seems. – Drill Bit Aug 02 '17 at 09:03
  • 1
    You really need to read the linked article. The imprecision is not coming from where you think it's coming from, and it's not actually going away in your solution. – Sneftel Aug 02 '17 at 11:49
  • @Sneftel After more tinkering I realize it did not do what it showed itself to do initially. But now I realize float is really, really bad. a = 1.2345 b = 0.0001 for i in range(10): a+= b print(a) it gives 1.2348 two times(!) with both your solution and mine – Drill Bit Aug 03 '17 at 10:07
  • @DrillBit: If it really did print `1.2348` twice, that would be a serious problem. But it doesn't. On Python 3, on my machine, it prints `1.2348` followed by `1.2348999999999999`. – Mark Dickinson Aug 03 '17 at 10:16
  • I can never use float again. Only large integers that are divided by 10^n just when they are needed. There is to much imprecision in float. Thank you all for the help. – Drill Bit Aug 18 '17 at 13:17