List:Bugs« Previous MessageNext Message »
From:George Whiffen Date:December 30 2002 9:23pm
Subject:Round (floating point precision) bug
View as plain text  
Subject: Round (floating point precision) bug

 >Description:

select if(round(0.1,1) = round(0.1,1),'ok','buggs!');

returns a result of "buggs". If 0.1 is replaced by 0.5 this returns ok, 
but 0.25 also generates a bug.  It is also interesting to note that
0.1 = round(0.1,1) gives a different result from round(0.1,1) = 0.1

 >How-To-Repeat:

	select if(round(0.1,1) = round(0.1,1),'ok','buggs!');:

 >Fix:

Compile item_func.cc without optimisation i.e. -O0 instead of -O3.

My guess is that the optimiser is keeping an intermediate result
on a floating point register, where it is held in extended format
rather than bringing it back to memory where it would be changed
back to a true C double. My best guess as to which variable is
being "bugged" by the optimiser would be tmp in Item_func_round::val(),
	
That should be enough to get you going, let me know if you need
more but be warned that I'm an application developer not a
software engineer.

You should note that this also fixes the problems with round
described in manual section A.5.7 where it is claimed that
the floating point comparison problem "cannot be solved by using
ROUND() (or similar function), because the result is still a
floating-point number".

More to come on the mailing list about this so-called "floating point 
comparison problem" and why the only proper solution is decimal rounding 
BY MYSQL as arithmetic errors are generated!
	
 >Submitter-Id:	george@stripped
 >Originator:	george@stripped
 >Organization:
  <organization of PR author (multiple lines)>
 >MySQL support: [none]
 >Synopsis:	round producing inconsistent results on same operands.
 >Severity:	serious
 >Priority:	
 >Category:	mysql
 >Class:		sw-bug
 >Release:	mysql-3.23.49 (Source distribution)

 >Environment:
i686, Linux 2.4.8-26mdk,gcc version 2.96 20000731 (Mandrake Linux 8.1 
2.96-0.62mdk)

System: Linux roi-47a 2.4.8-26mdk #1 Sun Sep 23 17:06:39 CEST 2001 i686 
unknown
Architecture: i686

Some paths:  /usr/bin/perl /usr/bin/make /usr/bin/gmake /usr/bin/gcc 
/usr/bin/cc
GCC: Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96/specs
gcc version 2.96 20000731 (Mandrake Linux 8.1 2.96-0.62mdk)



Thread
Round (floating point precision) bugGeorge Whiffen30 Dec
  • Re: Round (floating point precision) bugPeter Zaitsev3 Jan