JavaScript SpeechSynthesisUtterance

Viewed 69

Why does the SpeechSynthesisUtterance.volume property return "0.4000000059604645" when set to ".4"?

var u = new SpeechSynthesisUtterance();
u.volume = .4;
console.log(u.volume);

var u = new SpeechSynthesisUtterance();
u.volume = .4;
console.log(u.volume);
1 Answers

The .volume property is specified to be stored as a "float", which means that it's stored per single-precision 32 bit IEEE 754.

If you plug in 0.4, you get the hex value of 0x3ecccccd, which, when converted back to decimal, is 0.4000000059604645 (due to imprecision).

This is in contrast to normal JavaScript numbers, in which are stored as double-precision 64-bit numbers (and thus have many more accurate trailing digits).

Related