x-lang

← Index

x/num/float

IEEE 754 floating-point arithmetic, homed on the Float class.

Literal syntax: 3.14. The generic operators dispatch float operands

through the type ops; mixed operands resolve by the from-relation.

Conversion

float-split

int->float

float-frac

float-first-frac

float-int-digits

float-neg-int

libm-fn

str->float

float

Predicates

float?

float-of

Arithmetic

f-add

f-sub

f-mul

f-div

f-mod

Comparisons

f-lt

f-eq

Math Functions

fsin

fcos

fsqrt

fatan2

pi

ensure-float

float-type

R7RS Predicates

number?

Test whether a value is a number (now including floats).

Parameters:

Returns: BOOL — True if x is a number

Examples:

(list (number? 2.5) (number? "2.5")) => (#t #f)

Class Float

(Float float? x)

Test whether a value is a float.

Parameters:

Returns: BOOL — True if x is a float

Examples:

(list (Float float? 2.5) (Float float? 2)) => (#t #f)

(Float inexact? x)

Test whether a value is inexact. Equivalent to float?.

Parameters:

Returns: BOOL — True if x is a float

Examples:

(Float inexact? 0.5) => #t

(Float integer? x)

Test whether a value is an integer (the pre-float number? predicate).

Parameters:

Returns: BOOL — True if x is a native integer

Examples:

(list (Float integer? 3) (Float integer? 3.0)) => (#t #f)

(Float real? x)

Test whether a value is a real number (numbers minus complexes).

Parameters:

Returns: BOOL — True if x is real

Examples:

(Float real? 2.5) => #t

(Float bits->str bits)

Render a raw IEEE 754 bit pattern as its decimal string – FFI plumbing; value-level work wants (Float from) / the printer.

Parameters:

Returns: STRING — Decimal string representation

Examples:

(Float bits->str (Float int->bits 3)) => "3.0"

(Float bits->int bits)

Truncate a raw IEEE 754 bit pattern to a machine integer – FFI plumbing; value-level work wants (Float ->int).

Parameters:

Returns: INTEGER — Truncated integer value

Examples:

(Float bits->int (Float str->bits "2.5")) => 2

(Float int->bits n)

The IEEE 754 bit pattern of an integer’s double value – FFI plumbing, NOT a float constructor; (Float from) builds instances.

Parameters:

Returns: ANY — IEEE 754 double bit pattern: an INTEGER, or a VECTOR of two words on a 4-byte word

Examples:

(Float bits->str (Float int->bits 1)) => "1.0"

(Float str->bits s)

The IEEE 754 bit pattern of a decimal string’s double value – FFI plumbing, NOT a parser-to-instance; (Float from) builds instances (the old from-str name claimed FLOAT and returned bits, #66).

Parameters:

Returns: ANY — IEEE 754 double bit pattern: an INTEGER, or a VECTOR of two words on a 4-byte word

Examples:

(= (Float str->bits "1.0") (Float int->bits 1)) => #t

(Float from x)

Construct a float from any convertible value, through the conversion catalogue – the generic value door (was exact->inexact, #357). Raises a label ‘type when nothing converts.

Parameters:

Returns: FLOAT — Float instance

Examples:

(Float from 3) => 3.0

(Float ->int x)

Convert an inexact float to an exact integer by truncation.

Parameters:

Returns: INTEGER — Truncated integer value

Examples:

(Float ->int -2.7) => -2

(Float + a b)

Add two floats (other numerics promote).

Parameters:

Returns: FLOAT — Sum

Examples:

(Float + 1 0.5) => 1.5

(Float - a b)

Subtract two floats (other numerics promote).

Parameters:

Returns: FLOAT — Difference

Examples:

(Float - 3 0.5) => 2.5

(Float * a b)

Multiply two floats (other numerics promote).

Parameters:

Returns: FLOAT — Product

Examples:

(Float * 2 1.25) => 2.5

(Float / a b)

Divide two floats (other numerics promote).

Parameters:

Returns: FLOAT — Quotient

Examples:

(Float / 1 4) => 0.25

(Float < a b)

Test whether a is less than b (other numerics promote).

Parameters:

Returns: BOOL — True if a < b

Examples:

(Float < 1 1.5) => #t

(Float = a b)

Test whether a equals b (other numerics promote).

Parameters:

Returns: BOOL — True if a equals b

Examples:

(Float = 2 2.0) => #t

(Float sin x)

Compute the sine of a float.

Parameters:

Returns: FLOAT — Sine of x

Examples:

(Float sin 0.0) => 0.0

(Float cos x)

Compute the cosine of a float.

Parameters:

Returns: FLOAT — Cosine of x

Examples:

(Float cos 0.0) => 1.0

(Float tan x)

Compute the tangent of a float.

Parameters:

Returns: FLOAT — Tangent of x

Examples:

(Float tan 0.0) => 0.0

(Float sqrt x)

Compute the square root of a float.

Parameters:

Returns: FLOAT — Square root of x

Examples:

(Float sqrt 16.0) => 4.0

(Float exp x)

Compute e raised to a power.

Parameters:

Returns: FLOAT — e raised to the power x

Examples:

(Float exp 0.0) => 1.0

(Float log x)

Compute the natural logarithm of a float.

Parameters:

Returns: FLOAT — Natural logarithm of x

Examples:

(Float log 1.0) => 0.0

(Float abs x)

Compute the absolute value of a float.

Parameters:

Returns: FLOAT — Absolute value of x

Examples:

(Float abs -2.5) => 2.5

(Float floor x)

Round a float down to the nearest integer.

Parameters:

Returns: FLOAT — Largest integer not greater than x

Examples:

(Float floor -2.5) => -3.0

(Float ceil x)

Round a float up to the nearest integer.

Parameters:

Returns: FLOAT — Smallest integer not less than x

Examples:

(Float ceil 2.1) => 3.0

(Float round x)

Round a float to the nearest integer.

Parameters:

Returns: FLOAT — Nearest integer, ties away from zero

Examples:

(Float round 2.5) => 3.0

(Float trunc x)

Truncate a float toward zero.

Parameters:

Returns: FLOAT — Integer part of x

Examples:

(Float trunc -2.7) => -2.0

(Float rint x)

Round a float to the nearest integer using the current rounding mode.

Parameters:

Returns: FLOAT — Nearest integer

Examples:

(Float rint 2.5) => 2.0

(Float asin x)

Compute the arc sine of a float.

Parameters:

Returns: FLOAT — Arc sine in radians

Examples:

(Float asin 0.0) => 0.0

(Float acos x)

Compute the arc cosine of a float.

Parameters:

Returns: FLOAT — Arc cosine in radians

Examples:

(Float acos 1.0) => 0.0

(Float atan x)

Compute the arc tangent of a float.

Parameters:

Returns: FLOAT — Arc tangent in radians

Examples:

(Float atan 0.0) => 0.0

(Float pow base exponent)

Raise a float to a power.

Parameters:

Returns: FLOAT — base raised to the power exponent

Examples:

(Float pow 2.0 10.0) => 1024.0

(Float atan2 y x)

Compute the arc tangent of y/x, using signs to determine the quadrant.

Parameters:

Returns: FLOAT — Angle in radians

Examples:

(Float atan2 0.0 1.0) => 0.0

(Float log2 x)

Compute the base-2 logarithm of a float.

Parameters:

Returns: FLOAT — log2(x)

(Float log10 x)

Compute the base-10 logarithm of a float.

Parameters:

Returns: FLOAT — log10(x)

(Float hypot x y)

Compute sqrt(x^2 + y^2) without intermediate overflow (libm hypot).

Parameters:

Returns: FLOAT — The hypotenuse

(Float type)

The float type handle, for Convert and Type.

Returns: ATOM — The FLOAT type handle

(Float pi)

The circle constant pi, 3.14159265…

Returns: FLOAT — pi

(Float e)

Euler’s number e, 2.71828182…

Returns: FLOAT — e

(Float tau)

The turn constant tau = 2*pi, 6.28318530…

Returns: FLOAT — tau

(Float nan? x)

Is x a float NaN? #f for every non-float (an int is never NaN).

Parameters:

Returns: BOOL — #t only for a NaN float

(Float inf? x)

Is x a float infinity, either sign? #f for every non-float.

Parameters:

Returns: BOOL — #t only for an infinite float

(Float finite? x)

Is x a finite number? #t for machine INTs and finite floats; #f for float inf/NaN and for everything else (rational/bigint instances answer through their own classes).

Parameters:

Returns: BOOL — #t for machine ints and finite floats