Skip to main content
Version: Next

Float

Literal Specific Methods​

abs()​

Returns FLOAT

Returns the absolute value, as a float.

3.14.abs()
(0.0 - 3.14).abs()
Output
3.14
3.14

acos()​

Returns FLOAT

Returns the arccosine, in radians, of the number, as a float.

1.0.acos()
Output
0.0

asin()​

Returns FLOAT

Returns the arcsine, in radians, of the number, as a float.

0.0.asin()
Output
0.0

atan()​

Returns FLOAT

Returns the arctangent, in radians, of the number, as a float.

0.0.atan()
Output
0.0

ceil([INTEGER])​

Returns FLOAT

Returns the smallest float that is not less than the number. An optional digit count says how many decimal places to keep; a negative count rounds to a power of ten. The result stays a FLOAT -- in Ruby it would be an INTEGER.

1.5.ceil()
1.561.ceil(2)
555.5.ceil(0 - 1)
Output
2.0
1.57
560.0

copysign(NUMERIC)​

Returns FLOAT

Returns a float with the magnitude of the number and the sign of the argument.

3.2.copysign(0 - 1.0)
Output
-3.2

cos()​

Returns FLOAT

Returns the cosine, in radians, of the number, as a float.

0.0.cos()
Output
1.0

divmod(FLOAT)​

Returns ARRAY|ERROR

Returns the quotient and the remainder as a two-element array. Truncated toward zero, so it agrees with Integer#divmod and with /.

11.0.divmod(4.0)
11.0.divmod(0.0 - 4.0)
Output
[2.0, 3.0]
[-2.0, 3.0]

exp()​

Returns FLOAT

Returns e raised to the number, as a float.

1.0.exp()
Output
2.718281828459045

finite?()​

Returns BOOLEAN

Returns true when the number is neither infinite nor not-a-number.

1.5.finite?()
Output
true

floor([INTEGER])​

Returns FLOAT

Returns the largest float that is not greater than the number. Takes the same optional digit count as ceil.

1.5.floor()
1.567.floor(2)
(0.0 - 1.5).floor()
Output
1.0
1.56
-2.0

infinite?()​

Returns INTEGER|NIL

Returns 1 for positive infinity, -1 for negative infinity and nil otherwise, so the direction is not lost. Use finite? for the plain yes-or-no question.

1.5.infinite?()
Output
nil

log()​

Returns FLOAT

Returns the natural logarithm of the number, as a float.

1.0.log()
Output
0.0

log10()​

Returns FLOAT

Returns the decimal logarithm of the number, as a float.

100.0.log10()
Output
2.0

log2()​

Returns FLOAT

Returns the binary logarithm of the number, as a float.

8.0.log2()
Output
3.0

nan?()​

Returns BOOLEAN

Returns true when the number is not a number.

1.5.nan?()
Output
false

negative?()​

Returns BOOLEAN

Returns true when the number is less than zero.

(0.0 - 1.5).negative?()
0.0.negative?()
Output
true
false

positive?()​

Returns BOOLEAN

Returns true when the number is greater than zero.

1.5.positive?()
0.0.positive?()
Output
true
false

pow(NUMERIC)​

Returns FLOAT

Returns the number raised to the given power, as a float. Unlike Integer#pow there is no modulus argument.

2.0.pow(3.0)
Output
8.0

remainder(NUMERIC)​

Returns FLOAT

Returns the IEEE 754 floating-point remainder of the number divided by the argument, as a float.

100.0.remainder(30.0)
Output
10.0

round([INTEGER])​

Returns FLOAT

Returns the number rounded to the nearest value, halves away from zero. Takes the same optional digit count as ceil.

1.5.round()
1.567.round(2)
555.5.round(0 - 1)
Output
2.0
1.57
560.0

sin()​

Returns FLOAT

Returns the sine, in radians, of the number, as a float.

0.0.sin()
Output
0.0

sqrt()​

Returns FLOAT

Returns the square root of the number, as a float.

16.0.sqrt()
Output
4.0

tan()​

Returns FLOAT

Returns the tangent, in radians, of the number, as a float.

0.0.tan()
Output
0.0

truncate([INTEGER])​

Returns FLOAT

Returns the number with its fractional part dropped, rounding toward zero. Unlike floor this moves a negative number up. Takes the same optional digit count as ceil.

1.567.truncate()
(0.0 - 1.5).truncate()
1.567.truncate(2)
Output
1.0
-1.0
1.56

zero?()​

Returns BOOLEAN

Returns true when the number is zero.

0.0.zero?()
1.5.zero?()
Output
true
false

Generic Literal Methods​

help()​

Returns NIL

Prints the type's literal-specific methods with their argument and return types, sorted by name, one per line. It returns nil rather than the listing: this exists to be read, and the REPL echoes a returned value through its escaped representation, which would put the whole thing on one line. Use methods when the names are wanted as data. A type with no methods of its own prints only the heading.

true.help()
1.0.help()
Output
BOOLEAN supports the following methods:
nil
FLOAT supports the following methods:
abs()
acos()
asin()
atan()
ceil([INTEGER])
copysign(NUMERIC)
cos()
divmod(FLOAT)
exp()
finite?()
floor([INTEGER])
infinite?()
log()
log10()
log2()
nan?()
negative?()
positive?()
pow(NUMERIC)
remainder(NUMERIC)
round([INTEGER])
sin()
sqrt()
tan()
truncate([INTEGER])
zero?()
nil

is_a?(STRING)​

Returns BOOLEAN|ERROR

Returns true when the value is of the given type or belongs to the given type group, so one question covers both. The name has to be one that exists: anything else is an error rather than a false, because a typo would otherwise read as a real answer. See Types and type groups.

nil.is_a?("NIL")
"a".is_a?("HASHABLE")
nil.is_a?("HASHABLE")
true.is_a?("INTEGERABLE")
"a".is_a?("INTEGER")
Output
true
true
false
true
false

methods()​

Returns ARRAY

Returns the names of the methods specific to this literal type, not including the generic methods listed on this page. The names are sorted, so the result is the same on every run. A type with no methods of its own returns an empty array.

1.0.methods().contains?("round")
true.methods()
Output
true
[]

nil?()​

Returns BOOLEAN

Returns true only for nil. Reads better than comparing against nil at the end of a chain, and every type answers it.

nil.nil?()
1.nil?()
"".nil?()
[].first().nil?()
Output
true
false
false
true

to_float()​

Returns FLOAT|NIL

Converts an object to its float representation, or nil when it cannot. A nil result is what distinguishes a failed conversion from a genuine 0.0.

1.to_float()
"1.4".to_float()
"abc".to_float()
nil.to_float()
Output
1.0
1.4
nil
nil

to_integer()​

Returns INTEGER|NIL

Converts an object to its integer representation, or nil when it cannot. A nil result is what distinguishes a failed conversion from a genuine 0. For strings a 0b, 0o or 0x prefix selects binary, octal or hexadecimal and is matched case insensitively, a leading zero followed only by octal digits is octal, and anything else is decimal. The resulting integer keeps the base it was parsed with, and integers of differing bases cannot be combined directly.

true.to_integer()
false.to_integer()
1234.to_integer()
"4".to_integer()
"0".to_integer()
"0125".to_integer()
"0x2322".to_integer()
"0b1010".to_integer()
"test".to_integer()
Output
1
0
1234
4
0
0o125
0x2322
0b1010
nil

to_json()​

Returns STRING|ERROR

Returns the object as json notation.

a = {"test": 1234}
a.to_json()
Output
{"test": 1234}
"{\"test\":1234}"

to_string()​

Returns STRING

Converts an object to its string representation, or the empty string when it has none. Takes no arguments; an integer renders in its own base, so use to_base first to change it.

true.to_string()
1234.to_string()
"test".to_string()
1.4.to_string()
nil.to_string()
"0b1010".to_integer().to_string()
Output
"true"
"1234"
"test"
"1.4"
""
"0b1010"

type()​

Returns STRING

Returns the type of the object.

"test".type()
Output
"STRING"

type_groups()​

Returns ARRAY

Returns the type groups the value belongs to, sorted. ANY is not listed: every value belongs to it, so it would say nothing while prefixing every answer. It exists for signatures, where append(ANY) means the argument accepts anything, and is_a?("ANY") still answers true. See Types and type groups for what each group means.

1.type_groups()
nil.type_groups()
def() end.type_groups()
print.type_groups()
Output
["COMPARABLE", "HASHABLE", "INTEGERABLE", "NUMERIC", "STRINGABLE"]
["STRINGABLE"]
["CALLABLE"]
["CALLABLE"]

wat()​

Returns NIL

An alias of help, kept as an easter egg. This is the only alias in RocketLang; every other method has exactly one name.

true.wat()
1.0.wat()
Output
BOOLEAN supports the following methods:
nil
FLOAT supports the following methods:
abs()
acos()
asin()
atan()
ceil([INTEGER])
copysign(NUMERIC)
cos()
divmod(FLOAT)
exp()
finite?()
floor([INTEGER])
infinite?()
log()
log10()
log2()
nan?()
negative?()
positive?()
pow(NUMERIC)
remainder(NUMERIC)
round([INTEGER])
sin()
sqrt()
tan()
truncate([INTEGER])
zero?()
nil