Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Math.Round/Floor/Ceiling return long or int?

Tags:

Every time I use Math.Round/Floor/Ceiling I always cast to int (or perhaps long if necessary). Why exactly do they return double if it's always returning an integer.

like image 650
TheCloudlessSky Avatar asked Aug 14 '10 01:08

TheCloudlessSky


People also ask

Does Math round return a long?

round() is a built-in math function which returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result after adding 1/2, and casting the result to type long. If the argument is NaN, the result is 0.

Does round return an INT?

Round(decimal) will always return an integral value, these overloads still cannot return an integer value type.

Does Math floor round up or down?

The Math. floor() method rounds a number DOWN to the nearest integer.

Does casting to INT round up or down?

The cast rounds it "down" toward zero, while Math. floor rounds towards negative infinity.


1 Answers

The result might not fit into an int (or a long). The range of a double is much greater.

Approximate range of double: ±5.0 × 10−324 to ±1.7 × 10308

(Source)

like image 178
Mark Byers Avatar answered Sep 25 '22 06:09

Mark Byers