0

My Code is :

for(i <- -8.0 to 8.0 by 0.1) println(i)

Output is :

-8.0
-7.9
-7.800000000000001
-7.700000000000001
-7.600000000000001
-7.500000000000002
-7.400000000000002
-7.3000000000000025
-7.200000000000003
-7.100000000000003
-7.0000000000000036
-6.900000000000004
-6.800000000000004
-6.700000000000005
-6.600000000000005
-6.500000000000005
-6.400000000000006
-6.300000000000006
-6.200000000000006
-6.100000000000007
-6.000000000000007
-5.9000000000000075
-5.800000000000008
-5.700000000000008
-5.6000000000000085
-5.500000000000009
-5.400000000000009
-5.30000000000001
-5.20000000000001
-5.10000000000001
-5.000000000000011
-4.900000000000011
-4.800000000000011
-4.700000000000012
-4.600000000000012
-4.500000000000012
-4.400000000000013
-4.300000000000013
-4.2000000000000135
-4.100000000000014
-4.000000000000014
-3.900000000000014
-3.800000000000014
-3.700000000000014
-3.600000000000014
-3.5000000000000138
-3.4000000000000137
-3.3000000000000136
-3.2000000000000135
-3.1000000000000134
-3.0000000000000133
-2.9000000000000132
-2.800000000000013
-2.700000000000013
-2.600000000000013
-2.500000000000013
-2.400000000000013
-2.3000000000000127
-2.2000000000000126
-2.1000000000000125
-2.0000000000000124
-1.9000000000000123
-1.8000000000000123
-1.7000000000000122
-1.600000000000012
-1.500000000000012
-1.400000000000012
-1.3000000000000118
-1.2000000000000117
-1.1000000000000116
-1.0000000000000115
-0.9000000000000116
-0.8000000000000116
-0.7000000000000116
-0.6000000000000116
-0.5000000000000117
-0.4000000000000117
-0.3000000000000117
-0.2000000000000117
-0.10000000000001169
-1.1685097334179773E-14
0.09999999999998832
0.19999999999998833
0.29999999999998833
0.39999999999998836
0.49999999999998834
0.5999999999999883
0.6999999999999883
0.7999999999999883
0.8999999999999883
0.9999999999999882
1.0999999999999883
1.1999999999999884
1.2999999999999885
1.3999999999999886
1.4999999999999887
1.5999999999999888
1.6999999999999889
1.799999999999989
1.899999999999989
1.9999999999999891
2.099999999999989
2.199999999999989
2.299999999999989
2.3999999999999893
2.4999999999999893
2.5999999999999894
2.6999999999999895
2.7999999999999896
2.8999999999999897
2.99999999999999
3.09999999999999
3.19999999999999
3.29999999999999
3.39999999999999
3.4999999999999902
3.5999999999999903
3.6999999999999904
3.7999999999999905
3.8999999999999906
3.9999999999999907
4.099999999999991
4.19999999999999
4.29999999999999
4.39999999999999
4.499999999999989
4.599999999999989
4.699999999999989
4.799999999999988
4.899999999999988
4.999999999999988
5.099999999999987
5.199999999999987
5.2999999999999865
5.399999999999986
5.499999999999986
5.599999999999985
5.699999999999985
5.799999999999985
5.899999999999984
5.999999999999984
6.099999999999984
6.199999999999983
6.299999999999983
6.399999999999983
6.499999999999982
6.599999999999982
6.6999999999999815
6.799999999999981
6.899999999999981
6.9999999999999805
7.09999999999998
7.19999999999998
7.299999999999979
7.399999999999979
7.499999999999979
7.599999999999978
7.699999999999978
7.799999999999978
7.899999999999977
7.999999999999977

I expect the result as:

(-8.0, -7.9, -7.8, -7.7......., 7.7, 7.8, 7.9, 8.0)

How should I resolve this?

arco444
  • 22,002
  • 12
  • 63
  • 67

1 Answers1

2

Your expectation is wrong. You assume that floating point numbers work the same way in computers as they do in "pure maths".

Well, they don't, see here.

So you have to understand that your loop construct itself

for(i <- -8.0 to 8.0 by 0.1)

doesn't function as you expect it would.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I'm not sure I agree it's just a string formatting problem. – The Archetypal Paul Jan 18 '17 at 13:56
  • 1
    But it's not. It's about the inherent inaccuracy of floating-point. – The Archetypal Paul Jan 18 '17 at 13:59
  • "What you need: rounding / resp. formatting of your double values." and "in order to get your expected output, you have to turn all values into strings" is wrong. Continuing to do things that are inherently inaccurate and fudging it in the output is not the right way forward (IMO, obviously). Depending on the exact values, something like `for(i <- -8.0 to 8.0 by 0.1)` can produce an unexpected number of elements (because the expected end test fails due to the inaccuracy), and no amount of rounding/formatting/converting the double values into strings is going to fix that. – The Archetypal Paul Jan 18 '17 at 14:31
  • .. it's much better to understand why there are the inaccuracies and alter your approach to be able to deal with it. – The Archetypal Paul Jan 18 '17 at 14:37
  • No, floating point numbers do work exactly the same way in the computer as they (i.e., floating point numbers) do in pure math. It's just that when you write 0.1 in math, you don't mean a floating point number, and certainly not a binary floating point number. – Alexey Romanov Jan 18 '17 at 16:18