开发者

Determine if a float number is irrational of a fraction

开发者 https://www.devze.com 2023-02-13 04:47 出处:网络
I have a random float number and I have to determine if it is an irrati开发者_开发技巧onal number like √2 or a fraction like 123/321. Both of them are represented like an endless set of numbers anywh

I have a random float number and I have to determine if it is an irrati开发者_开发技巧onal number like √2 or a fraction like 123/321. Both of them are represented like an endless set of numbers anywhere but is there any way to definitely say whether a number is a fraction or it's irrational?

Thank you!


All floating point numbers are rational because the mantissa has a fixed length. Irrational numbers stored in floating point are truncated into rational numbers.

If you have a specific list of numbers you need to match, you can compare the random number to numbers on the list at a set floating point precision, but do keep in mind that you will get false positives due to truncation or rounding.


All (finite, non-NaN) floating point values are rational, because all finite (binary) floating-point numbers are of the form f*2^e for integers f and e.

0

精彩评论

暂无评论...
验证码 换一张
取 消