Firefox and Javascript Rounding Rules -
i don't know if i'm missing obvious here but...
in ie, opera , chrome, expect rounding numbers ending in 5:
125 toprecision(2) => 130 11.5 toprecision(2) => 12 this i'd expect.
firefox, though, little more 'sophisticated' yielding following:
125 toprecision(2) => 120 //wtf!!! 11.5 toprecision(2) => 12 after bit of head scratching, i've come conclusion firefox using 'rounding even' rule where, if digit before 5 number rounds down , if digit before 5 odd number rounds up:
0.5 => 0 1.5 => 2 2.5 => 2 3.5 => 4, etc. i using rounded results test student solutions engineering questions pseudo-randomly generated question inputs. question input in chrome h=1020 mm h=1030 mm in ff, chrome or opera.
i need function make rounding consistent, i.e. want 0.0001235 round 0.000124 , want 1234 round 1240 can't use simple num = math.floor(num + 0.5); complicate matters bit, want input variables , student answers correct 3 sig digs unless first digit 1, in case want 4 sig digs:
234.5 => 235 134.5 => 134.5 i've hacked solution 3 or 4 sig digs depending upon first digit converting number string , testing first non-zero, non-decimal point , non-negative character '1' - not pretty, works. similar rounding problem, checking whether digit rounded 5 i'm wondering if there elegant bit-wise solution.
please take @ tests here
http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/
javascript has single number type: ieee 754 double precision floating point. having single number type 1 of javascript’s best features. multiple number types can source of complexity, confusion, , error. single type simplifying , stabilizing.
unfortunately, binary floating point type has significant disadvantages. worst cannot accurately represent decimal fractions, big problem because humanity has been doing commerce in decimals long, long time. there advantages switching binary-based number system, not going happen. consequence, 0.1 + 0.2 === 0.3 false, source of lot of confusion.
also take @ questions:
and
https://stackoverflow.com/questions/744099/javascript-bigdecimal-library/1575569#1575569
Comments
Post a Comment