java 为什么没有 Math.floor(float)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6060940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:09:58  来源:igfitidea点击:

why isn't there a Math.floor(float)?

java

提问by Bick

Why is there only Math.floor(double)?

为什么只有 Math.floor(double)?

I have a float and I want to round it "down".
do I have to cast it to double?

我有一个浮点数,我想把它“向下”四舍五入。
我必须把它加倍吗?

采纳答案by Oliver Charlesworth

It will be converted automatically (see this on widening primitive conversions). If you want the result as a float, however, you will need to explicitly cast the return value.

它将自动转换(请参阅有关扩展原始转换的内容)。float但是,如果您希望结果为,则需要显式转换返回值。

回答by Brendan K

Yes, but when speed is critical support for single precision floats should be provided. There should be a single precision analogous for java.lang.Math

是的,但是当速度至关重要时,应该提供对单精度浮点数的支持。应该有一个类似于 java.lang.Math 的单精度

回答by Michael

No, a floatprimitive will automatically be cast to a doublewithout the loss of any precision.

不,float原语将自动转换为 adouble而不会损失任何精度。

回答by Himadri Choudhury

A double is big enough to exactly represent every single possible float and more. You are not going to lose any precision, the cast happens automatically.

double 足够大,可以准确地表示每一个可能的浮点数等等。您不会失去任何精度,演员表会自动发生。

回答by Bala R

floatand double, both are floating point data types with double having larger range. You should be able to use your float variable with Math.Floor(double) without any problems.

floatdouble, 都是浮点数据类型,double 的范围更大。您应该能够在 Math.Floor(double) 中使用您的 float 变量而不会出现任何问题。

回答by Richard H

You can just pass the float without doing a cast as a float has less precision than a double.

您可以只传递浮点数而不进行强制转换,因为浮点数的精度低于双精度数。