struct const class sys::Float

sys::Obj
  sys::Num
    sys::Float

@Serializable { simple=true }

Float is used to represent a 64-bit floating point number.

bits

Int bits()

Return 64-bit representation according IEEE 754 floating-point double format bit layout. This method is paired with Float.makeBits.

bits32

Int bits32()

Return 32-bit representation according IEEE 754 floating-point single format bit layout. This method is paired with Float.makeBits32.

compare

virtual override Int compare(Obj obj)

Compare based on floating point value.

NaN works as follows:

  • for the <=> operator NaN is always less than other values and equal to itself (so sort works as expected)
  • for all other comparison operators anything compared against NaN is false (normal Java semanatics)

Examples:

Float.nan <=> Float.nan  =>  0
2f <=> Float.nan         =>  1
Float.nan <=> 2f         =>  -1
2f < Float.nan           =>  false
Float.nan < 2f           =>  false
Float.nan <= Float.nan   =>  false
decrement

@Operator
Float decrement()

Decrement by one. Shortcut is --a or a--.

defVal

const static Float defVal := 0.0f

Default value is 0f.

div

@Operator
Float div(Float b)

Divide this by b. Shortcut is a/b.

divInt

@Operator
Float divInt(Int b)

Divide this by b. Shortcut is a/b.

equals

virtual override Bool equals(Obj? obj)

Return true if same float value. Like Java, NaN != NaN. Also see compare.

fromStr

static new fromStr(Str s, Bool checked := true)

Parse a Str into a Float. Representations for infinity and not-a-number are "-INF", "INF", "NaN". This string format matches the lexical representation of Section 3.2.5 of XML Schema Part 2. If invalid format and checked is false return null, otherwise throw ParseErr.

hash

virtual override Int hash()

Return bits().

increment

@Operator
Float increment()

Increment by one. Shortcut is ++a or a++.

isNaN

Bool isNaN()

Return if this is Float.nan. Also see compare.

isNegZero

Bool isNegZero()

Return if this is negative zero value.

makeBits

static Float makeBits(Int bits)

Make a Float for the specified 64-bit representation according IEEE 754 floating-point double format bit layout. This method is paired with Float.bits.

makeBits32

static Float makeBits32(Int bits)

Make a Float for the specified 32-bit representation according IEEE 754 floating-point single format bit layout. This method is paired with Float.bits32.

minus

@Operator
Float minus(Float b)

Subtract b from this. Shortcut is a-b.

minusInt

@Operator
Float minusInt(Int b)

Subtract b from this. Shortcut is a-b.

mod

@Operator
Float mod(Float b)

Return remainder of this divided by b. Shortcut is a%b.

modInt

@Operator
Float modInt(Int b)

Return remainder of this divided by b. Shortcut is a%b.

mult

@Operator
Float mult(Float b)

Multiply this with b. Shortcut is a*b.

multInt

@Operator
Float multInt(Int b)

Multiply this with b. Shortcut is a*b.

nan

const static Float nan := 0.0 / 0.0

Float value for Not-A-Number.

negInf

const static Float negInf := -1.0 / 0.0

Float value for negative infinity.

negate

@Operator
Float negate()

Negative of this. Shortcut is -a.

normNegZero

Float normNegZero()

If this value is negative zero then return normalized zero, otherwise return this value.

plus

@Operator
Float plus(Float b)

Add this with b. Shortcut is a+b.

plusInt

@Operator
Float plusInt(Int b)

Add this with b. Shortcut is a+b.

posInf

const static Float posInf := 1.0 / 0.0

Float value for positive infinity.

random

static Float random()

Generate a random float between 0.0 inclusive and 1.0 exclusive. Also see Int.random, Range.random, List.random, and Random.

toCode

Str toCode()

Get this Float as a Fantom code literal.

toFloat

virtual override Float toFloat()

Convert this number to a Float.

toInt

virtual override Int toInt()

Convert this number to an Int.

toLocale

Str toLocale(Str? pattern := null)

Format this floating point number for the current locale. If pattern is null, then the locale's default pattern is used. Also see Num.localeDecimal, Num.localeGrouping, etc.

The pattern format:

#   optional digit
0   required digit
.   decimal point
,   grouping separator (only last one before decimal matters)

Examples:

12345.786f.toLocale("#,###.0")  =>  12,345.8
7.1234f.toLocale("#.000")       =>  7.123
0.1234f.toLocale("#.000")       =>  .123
0.1234f.toLocale("0.00")        =>  0.12
70.12f.toLocale("0.0000")       =>  70.1200
toStr

virtual override Str toStr()

Get string representation according to the lexical representation defined by Section 3.2.5 of XML Schema Part 2. Representations for infinity and not-a-number are "-INF", "INF", "NaN".