Floats
| Item | Summary |
|---|---|
floor | Returns the largest whole number less than or equal to the number, as a float |
to_str | Returns the number as a string, using the fewest digits that still read back as the same float |
ceil | Returns the smallest whole number greater than or equal to the number, as a float |
round | Returns the nearest whole number, as a float |
to_int | Returns the number as an int, dropping anything after the decimal point |
abs | Returns the absolute value |
max | Returns the larger of the number and other |
min | Returns the smaller of the number and other |
sqrt | Returns the square root |
format | Returns the number as a string with exactly places digits after the decimal point |
clamp | Returns the number moved into the range from low to high, inclusive |
signum | Returns the sign of the number as 1.0 or -1.0 |
sin | Returns the sine of an angle in radians |
cos | Returns the cosine of an angle in radians |
tan | Returns the tangent of an angle in radians |
asin | Returns the arcsine in radians, between -PI / 2.0 and PI / 2.0 |
acos | Returns the arccosine in radians, between 0.0 and PI |
atan | Returns the arctangent in radians, between -PI / 2.0 and PI / 2.0 |
atan2 | Returns the angle in radians from the positive x axis to the point (x, y), where y is the number this is called on |
pow | Returns the number raised to the power exponent |
exp | Returns E raised to the power of the number |
ln | Returns the natural logarithm (base E) |
random | Returns a random float that is at least 0.0 and less than len |
Methods
floor
fn floor(self) -> float
Returns the largest whole number less than or equal to the number, as a float.
let a = 2.7.floor(); // 2.0
let b = (-2.7).floor(); // -3.0
to_str
fn to_str(self) -> str
Returns the number as a string, using the fewest digits that still read back as the same
float. A whole number has no .0. For a fixed number of decimal places, use
format.
let a = 0.1.to_str(); // "0.1"
let b = 2.0.to_str(); // "2"
let c = (0.1 + 0.2).to_str(); // "0.30000000000000004"
ceil
fn ceil(self) -> float
Returns the smallest whole number greater than or equal to the number, as a float.
let a = 2.2.ceil(); // 3.0
let b = (-2.2).ceil(); // -2.0
round
fn round(self) -> float
Returns the nearest whole number, as a float. A number exactly halfway between two whole
numbers rounds away from zero.
let a = 2.4.round(); // 2.0
let b = 2.5.round(); // 3.0
let c = (-2.5).round(); // -3.0
to_int
fn to_int(self) -> int
Returns the number as an int, dropping anything after the decimal point. Round first with
round to get the nearest int instead.
let a = 2.9.to_int(); // 2
let b = (-2.9).to_int(); // -2
let c = 2.9.round().to_int(); // 3
abs
fn abs(self) -> float
Returns the absolute value.
let a = (-1.5).abs(); // 1.5
max
fn max(self, other: float) -> float
Returns the larger of the number and other.
let a = 1.5.max(2.5); // 2.5
min
fn min(self, other: float) -> float
Returns the smaller of the number and other.
let a = 1.5.min(2.5); // 1.5
sqrt
fn sqrt(self) -> float
Returns the square root. A negative number gives NaN.
let a = 9.0.sqrt(); // 3.0
format
fn format(self, places: int) -> str
Returns the number as a string with exactly places digits after the decimal point. The last
digit is rounded, with a tie going to the even digit (2.5.format(0) is "2", but
3.5.format(0) is "4").
let a = 3.14159.format(2); // "3.14"
let b = 2.0.format(3); // "2.000"
clamp
fn clamp(self, low: float, high: float) -> float
Returns the number moved into the range from low to high, inclusive. A number already in
the range comes back unchanged.
A low greater than high, or either bound being NaN, is a runtime error.
let a = 1.5.clamp(0.0, 1.0); // 1.0
let b = (-0.2).clamp(0.0, 1.0); // 0.0
signum
fn signum(self) -> float
Returns the sign of the number as 1.0 or -1.0. Zero counts as positive (0.0.signum() is
1.0), unless it’s -0.0.
let a = (-3.2).signum(); // -1.0
let b = 5.0.signum(); // 1.0
sin
fn sin(self) -> float
Returns the sine of an angle in radians. To work in degrees, multiply by PI / 180.0 first.
use std::math::PI;
let a = (PI / 2.0).sin(); // 1.0
let b = (90.0 * PI / 180.0).sin(); // 1.0
cos
fn cos(self) -> float
Returns the cosine of an angle in radians.
use std::math::PI;
let a = PI.cos(); // -1.0
tan
fn tan(self) -> float
Returns the tangent of an angle in radians.
let a = 0.0.tan(); // 0.0
asin
fn asin(self) -> float
Returns the arcsine in radians, between -PI / 2.0 and PI / 2.0. A number outside -1.0 to
1.0 gives NaN.
let a = 1.0.asin(); // 1.5707963267948966 (PI / 2.0)
acos
fn acos(self) -> float
Returns the arccosine in radians, between 0.0 and PI. A number outside -1.0 to 1.0
gives NaN.
let a = 1.0.acos(); // 0.0
atan
fn atan(self) -> float
Returns the arctangent in radians, between -PI / 2.0 and PI / 2.0. To get an angle from a
point or direction, atan2 is usually the better choice.
let a = 1.0.atan(); // 0.7853981633974483 (PI / 4.0)
atan2
fn atan2(self, x: float) -> float
Returns the angle in radians from the positive x axis to the point (x, y), where y is the
number this is called on. The result is between -PI and PI.
Unlike (y / x).atan(), it uses the signs of both coordinates to find the right quadrant, and
an x of 0.0 is fine.
let a = 1.0.atan2(1.0); // 0.7853981633974483 (PI / 4.0)
let b = 1.0.atan2(-1.0); // 2.356194490192345 (3.0 * PI / 4.0)
pow
fn pow(self, exponent: float) -> float
Returns the number raised to the power exponent.
let a = 2.0.pow(10.0); // 1024.0
let b = 9.0.pow(0.5); // 3.0
exp
fn exp(self) -> float
Returns E raised to the power of the number.
let a = 1.0.exp(); // 2.718281828459045
ln
fn ln(self) -> float
Returns the natural logarithm (base E). 0.0 gives negative infinity, and a negative number
gives NaN.
use std::math::E;
let a = E.ln(); // 1.0
let b = 1.0.ln(); // 0.0
Associated Functions
random
fn random(len: float) -> float
Returns a random float that is at least 0.0 and less than len.
A len that isn’t a finite number greater than 0.0 is a runtime error.
if float::random(1.0) < 0.25 {
print("a one in four chance");
}