floating point - Lack of precision of the toFixed method in javascript -




i have test number.prototype.tofixed method in chrome(v60.0.3112.101) console , found sth puzzled me.

why 1.15.tofixed(1) return "1.1" not "1.2"?
why 1.05.tofixed(1) return "1.1" not "1.0"?
, on... test on chrome console


i research in ecmascript specification.

note 1 tofixed returns string containing number value represented in decimal fixed-point notation fractiondigits digits after decimal point. if fractiondigits undefined, 0 assumed.

i know what's fixed point notation.but can't explain puzzles above. give clear explaination?


btw, think details arithmetic under specification should improved. saying 1.105 instance, relative arithmetic following:

let n integer exact mathematical value of n ÷ 10^f - x close 0 possible. if there 2 such n, pick larger n.

according pick larger n, 111 should taken consideration not 110, contradicted reality.

i'll try best clarify points around question. first of fixed-point notation:

i know what's fixed point notation. can't explain well.

the fixed-point natation opposed floating point notation. floating point notation allow better precision of time. more difficult understand , compute.

well, let's go fixed-point notation. arithmetic notation real numbers. difference number in fixed-point notation represented integer scaling factor. example :

if want write 4.56 , got 1/1000 scaling factor, number represented 4560. indeed, 4560 * (1/1000) = 4.56

now know how fixed-point notation work, can better understand results of tofixed(n) function. let's example scaling factor 1/1000 (that not real value easier visualise results).

1.15.tofixed(1) 

will take on decimal , represent number fixed-point notation, not care '5'. number got in memory 1100. why number rounded closest inferior value.

now can see on mdn doc of tofixed function can keep 20 decimals. information can scaling factor 1/10^20.

i hope answers questions.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -