Sample Header Ad - 728x90

Selecting a floating value yields incorrect results

1 vote
1 answer
108 views
Consider the following. CREATE TABLE IF NOT EXISTS docs ( id int(6) unsigned NOT NULL, qty float NOT NULL, price float NOT NULL, rebate float not null, PRIMARY KEY (id) ) DEFAULT CHARSET=utf8; INSERT INTO docs VALUES (1,1,43616.7,0); SELECT SUM(qty * (price - price * rebate/100)) AS endval FROM docs Instead of returning 43616.7 as expected, this query gives back 43616.69921875 (on some machines 43616.671875). Demo fiddle can be found here . Why is this happening? I would expect it to work like this (apologies for actually typing out something as basic as this): 1 * (43616.7 - 43616.7 * 0 / 100) = 1 * (43616.7 - 0) = 43616.7 I realize that I could overcome the issue by going with decimal (10,2) for my columns, I'm just wondering what's causing this unexpected behaviour.
Asked by FiddlingAway (125 rep)
May 6, 2022, 07:15 PM
Last activity: May 7, 2022, 01:04 AM