[a119] Getting incorrect floats with division and multiplication maths and Map() Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
User avatar
Tigerlily
Posts: 362
Joined: 04 Oct 2018, 22:31

[a119] Getting incorrect floats with division and multiplication maths and Map()

07 Aug 2020, 20:30

[Moderator's note: Topic moved from Bug Reports.]

I haven't encountered this until relatively recently so i'm guessing it has to do with this release or the last few.. has anyone else had issues like this? Or maybe my code is wrong?

I'm getting weird long floats with seemingly arbitrary numbers. And also on another more simplified attempt to do this via a Map(), still getting weird conversions when there is no math even involved...

Here is my code:

Code: Select all

a := ""
r := []
y := [0,5,15,25,35,45,50,55,65,75,85,95,100]
for x in y
{
	r.Push((2.56 * x) - 128)
	a .= r[A_Index]  "`n" ; convert to decimal	
}

MsgBox a

b := ""
for c in r
	b .= ((c + 128) / 2.56) "`n" ; convert back to percentage

MsgBox b
Here is the output:

Code: Select all

; output a
-128.0
-115.2
-89.599999999999994
-64.0
-38.399999999999991
-12.799999999999997
0.0
12.800000000000011
38.400000000000006
64.0
89.599999999999994
115.20000000000002
128.0

; output b
0.0
4.9999999999999991
15.000000000000002
25.0
35.0
45.0
50.0
55.0
65.0
75.0
85.0
95.0
100.0
I have no idea what's going on here. When i do these same calculations with a hand/computer calculator, I get the correct outputs, which should be:

Code: Select all

; output a
-128.0
-115.2
-89.6
-64.0
-38.4
-12.8
0.0
12.8
38.4
64.0
89.5
115.2
128.0

; output b
0.0
5.0
15.0
25.0
35.0
45.0
50.0
55.0
65.0
75.0
85.0
95.0
100.0
Thx for your replies. :rainbow:
Last edited by Tigerlily on 07 Aug 2020, 20:41, edited 2 times in total.
-TL
User avatar
Tigerlily
Posts: 362
Joined: 04 Oct 2018, 22:31

Re: [a119] Getting incorrect floats with division and multiplication maths

07 Aug 2020, 20:38

This is another example I forgot I had taken a screenshot of a couple weeks back, showing a different issue, however seems related:

Image

For reference, I ran the above code with different array numbers matching this map and these were the outputs:

Image


Image

this code fixes this particular issue as a workaround, but this seems odd:

Code: Select all

a := ""
r := []
y := [0,10,20,30,40,50,60,70,80,90,100]
for x in y
{
	r.Push(Round(((2.56 * x) - 128), 1))
	a .= r[A_Index]  "`n" ; convert to decimal	
}

MsgBox a

b := ""
for c in r
	b .= Round(((c + 128) / 2.56), 1) "`n" ; convert back to percentage

MsgBox b
output:
Image


Image
Last edited by Tigerlily on 07 Aug 2020, 20:54, edited 1 time in total.
-TL
gregster
Posts: 5791
Joined: 30 Sep 2013, 06:48

Re: [a119] Getting incorrect floats with division and multiplication maths and Map()  Topic is solved

07 Aug 2020, 20:51

That computers are not ideally suited for floating point arithmetic is a well-known problem ;) : https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

Afaik, the automatic formatting that was done in v1 to handle these effects (hiding the inaccuracies) was removed in v2...

Related: https://www.autohotkey.com/boards/viewtopic.php?f=82&t=78578&p=342241#p341749
and https://lexikos.github.io/v2/docs/Concepts.htm#float-imprecision .
User avatar
Tigerlily
Posts: 362
Joined: 04 Oct 2018, 22:31

Re: [a119] Getting incorrect floats with division and multiplication maths and Map()

07 Aug 2020, 21:01

ah very interesting, as I had never encountered this problem before, even when data manipulating tons of statistics numbers (floating point numbers) with AHK and Excel.

I suppose the reason this happens with the Map() example as well is the computer having difficulty representing 102.4?? such a basic number. how interesting.


Thanks for the info
-TL
gregster
Posts: 5791
Joined: 30 Sep 2013, 06:48

Re: [a119] Getting incorrect floats with division and multiplication maths and Map()

07 Aug 2020, 21:04

Excel probably hides these inaccuracies by auto-formatting them away... like AHK v1 did, I guess.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 3 guests