本文介绍了确定一个浮点数是不是一个分数的非理性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个随机的浮点数,我必须确定它是一个无理数,如√2或像123/321这样的分数。他们都代表像一个无尽的数字在任何地方,但有什么方法可以肯定地说数字是一个分数还是非理性?

谢谢! p>

解决方案

所有数字是合理的,因为具有固定长度。如果你有一个你需要匹配的特定的数字列表,你可以比较随机数与数字上的数字。以设定的浮点精度列出,但请记住,由于截断或四舍五入,您将获得误报。


I have a random float number and I have to determine if it is an irrational 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.

这篇关于确定一个浮点数是不是一个分数的非理性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:16