Class float

extends num implements Comparable
A 64 bit floating point value.
Floats are double precision IEEE 754 values, including float.NAN, float.INFINITY, -float.INFINITY and negative zero.
This is a fully fledged class, not a 'primitive type'.
Floats are immutable objects.

Class summary


NAN -> float
A not-a-number representation.
INFINITY -> float
The infinity representation.
MAX-FINITE -> float
The maximum finite float.
MIN-POSITIVE -> float
The minimum positive float.
parse data/Data -> float
Parses the data to a float.
parse data/Data [--on-error] -> float
Deprecated. Use parse data [--if-error] instead.
parse data/Data [--if-error] -> float
Variant of parse data.
parse data/Data from/int to/int= -> float
Deprecated. Use parse data with slices instead.
from-bits raw/int -> float
Converts to raw bit pattern to the corresponding float.
from-bits32 raw/int -> float
Converts the given raw bits to a 32-bit floating-point number and returns the corresponding float.

Statics

Converts to raw bit pattern to the corresponding float.
This function is the inverse of bits.

Converts the given raw bits to a 32-bit floating-point number and returns the corresponding float.
Given the raw bits of an IEEE 754 single-precision (binary32) floating-point number, constructs the corresponding value, and returns it as a float.
This function is the inverse of bits32.

The infinity representation.

The maximum finite float.

The minimum positive float.

A not-a-number representation.
Use is-nan to check for not-a-number.
Advanced
There are multiple representations of not-a-number. For example, the
following produces another not-a-number representation:

float.from-bits (float.NAN.bits + 1)
Comparing the above representation with this constant will result in false:

float.NAN == float.from-bits (float.NAN.bits + 1)  // => false
It is therefore important to use is-nan to check for not-a-number.

parse data/Data -> float
Parses the data to a float.
Returns the nearest floating point number for data not representable by any floating point number.
Errors
The data must contain only a valid float. Trailing junk is not allowed.
The data must not be empty.
Examples

float.parse "2"          // => 2.0
float.parse "2.0"        // => 2.0
float.parse "2.1"        // => 2.1000000000000000888
float.parse "007"        // => 7.0
float.parse "anno 2017"  // Error.

parse data/Data [--on-error] -> float
Deprecated. Use parse data [--if-error] instead.

parse data/Data [--if-error] -> float
Variant of parse data.
If the data can't be parsed correctly, returns the result of calling the if-error block.

parse data/Data from/int to/int=data.byte-size -> float
Deprecated. Use parse data with slices instead.

Methods

See super.

Converts this number to its bit representation.
A float corresponds to the IEEE 754 double precision (binary64) type. It has 64 bits, of which 1 bit is used as sign, 11 for the exponent, and 52 for the significant.
This function is the inverse of from-bits.

Converts this instance to a 32-bit floating-point number and returns its bits.
Advanced
Internally converts this instance to a IEEE 754 single precision (binary32) value and returns its bits. The format consists of 1 sign bit, 8 exponent bits, and 23 significand bits.
The conversion from 64-bit floating-point number (this instance) to a 32-bit number loses in precision and range. If this instance is out of range it is mapped to the IEEE 754 single precision infinity value which has bit-pattern 0x7F80_0000 (positive) or 0xFF80_0000 (negative).

Returns the smallest integral value not less than this number.
If this value is not finite (NaN, infinity, or negative infinity), then returns this number.

compare-to other/num -> int
Compares this number to the other.
Uses the truncated division for the modulo computation. The sign of the result is thus always the same as the one of the divisor (the first operand).
Returns 1 if this number is greater than the other.
Returns 0 if this number is equal to the other.
Returns -1 if this number is less than the other.
Returns -1 if the other is NaN (float.NAN).
Returns 0 if both this number and the other are NaN (float.NAN).
Return 1 if this number is NaN and the other is not NaN.
Contrary to < this comparison handles 0.0 and -0.0, such that 0.0.compare-to -0.0 returns 1.
Examples

2.compare-to 1  // => 1
1.compare-to 1  // => 0
1.compare-to 2  // => -1

(-0.0).compare-to 0.0 // => -1

2.compare-to float.NAN // => -1

float.INFINITY.compare-to 3               // => 1
float.INFINITY.compare-to float.INFINITY  // => 0
3.compare-to float.INFINITY               // => -1

compare-to other/num [--if-equal] -> int
Variant of compare-to other.
Calls if-equal if this number is equal to other.
Examples
In the example, MyTime implements a lexicographical ordering of seconds and nanoseconds using compare-to other [--if-equal] to move on to nanoseconds when the seconds component is equal.

class MyTime:
  seconds/int
  nanoseconds/int

  constructor .seconds .nanoseconds:

  compare-to other/MyTime -> int:
    return seconds.compare-to other.seconds --if-equal=:
      nanoseconds.compare-to other.nanoseconds

Returns the largest integer not greater than this number.
If this value is not finite (NaN, infinity, or negative infinity), then returns this number.

Whether this number is finite.
Examples

2.0.is-finite                 // => true
(-9001.0).is-finite           // => true
2.sqrt.is-finite              // => true
float.MAX-FINITE.is-finite    // => true
float.MIN_POSITIVE.is-finite  // => true

float.NAN.is-finite       // => false
(-1).sqrt.is-finite       // => false
float.INFINITY.is-finite  // => false

Whether this number is a NaN (float.NAN).
Examples

float.NAN.is-nan                               // => true
(-1).sqrt.is-nan                               // => true
(float.from-bits (float.NAN.bits + 1)).is-nan  // => true

2.0.is-nan                 // => false
2.sqrt.is-nan              // => false
float.INFINITY.is-nan      // => false
float.MAX-FINITE.is-nan    // => false
float.MIN_POSITIVE.is-nan  // => false

operator - other/any -> float
See super.

operator * other/any -> float
See super.

operator / other/any -> float
See super.

operator % other/any -> float
See super.

operator + other/any -> float
See super.

operator < other/any -> bool
See super.

operator <= other/any -> bool
See super.

operator == other/any -> bool
See super.

operator > other/any -> bool
See super.

operator >= other/any -> bool
See super.

Rounds this number to the nearest integer.
Errors
This number must not be a NaN (float.NAN) or negative and positive infinity (float.INFINITY).
Examples

3.1.round  // => 3
3.4.round  // => 3
3.5.round  // => 4
3.9.round  // => 4

(-5.1).round  // => -5
(-5.4).round  // => -5
(-5.5).round  // => -6
(-5.9).round  // => -6

(-0.0).round  // => 0
2.sqrt.round  // => 1

round --precision/any -> float
Deprecated.

Returns the sign of this instance.
The sign is:
  • -1 for negative numbers, and for -0.0
  • 0 for 0.0
  • 1 for positive numbers.

stringify precision/any -> string
Deprecated. Use to-string --precision instead.

Converts this number to a floating point number.
For very large integers, the conversion may be to the nearest floating point number.
Examples

2.to-float   // => 2.0
2.1.to-float // => 2.1

9223372036854775807.to-float  // => 9223372036854775808.0


to-string --precision/int=null -> string
See super.
If precision is null, the shortest correct string is returned.
If precision is an integer format "%.*lf" in C++ is used.
Errors
The precision must be an integer in range [0..64] or null.

Rounds this to the nearest value that is not larger in magnitude than this number.
If this value is not finite (NaN, infinity, or negative infinity), then returns this number.