AssemblyScript / assemblyscript

A TypeScript-like language for WebAssembly.
https://www.assemblyscript.org
Apache License 2.0
16.97k stars 664 forks source link

f32 toString emits extra decimals #2873

Open mattjohnsonpint opened 2 months ago

mattjohnsonpint commented 2 months ago

Bug description

Calling toString on an f64 value works as expected, but doing the same on an f32 string introduces extra noise decimals. I suspect it is because the f32 value is being cast to f64 when passed into the dtoa function:

https://github.com/AssemblyScript/assemblyscript/blob/f79391c91a0875e98a6e887b3353210b4125dc87/std/assembly/number.ts#L317-L319

https://github.com/AssemblyScript/assemblyscript/blob/f79391c91a0875e98a6e887b3353210b4125dc87/std/assembly/util/number.ts#L734

Steps to reproduce

console.log((<f64>1.1).toString());  // "1.1"
console.log((<f32>1.1).toString());  // "1.100000023841858"

AssemblyScript version

v0.27.29

mattjohnsonpint commented 2 months ago

Perhaps not the cast itself, but rather dtoa is designed for 64-bit precision input value. If there were a 32-bit version of dtoa and its dependent functions, then it should be possible to emit "1.1" for an f32.