简体   繁体   中英

Procedure for Arithmetic (Division) in tcl

Need to divide two numbers (can be floating) in tcl and check if the number is an exact multiple.

!($x % $y) doesn't work as the operand expects integers.

Many floating point numbers used on computers are just an approximation of the specified value. So expecting to be able to check if one value is an exact multiple of another value will likely lead to disappointment.

For example: expr {fmod(1, 0.1)} => 0.09999999999999995 because 0.1 cannot be represented exactly in binary floating point format.

I'm afraid you will have to reconsider your requirements.

See also https://en.wikipedia.org/wiki/Floating-point_arithmetic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM