module HexFloat

Overview

Provides conversion functions between hexadecimal floating-point literals and floating-point values.

Defined in:

hexfloat.cr

Constant Summary

VERSION = "1.0.0"

Class Method Summary

Macro Summary

Class Method Detail

def self.to_f32(str : String) : Float32 #

Converts a hexadecimal floating-point literal to a Float32.

The literal must match /\A-?0x[0-9A-Fa-f]+(\.[0-9A-Fa-f]+)?p([+-])?[0-9]+(_?f32)?\z/. Inexact literals are rounded to the nearest representable Float32, ties-to-even.

Does not support infinity and not-a-number.

HexFloat.to_f32("0x12.34p+5")      # => 582.5
HexFloat.to_f32("-0x0.555p-2_f32") # => -0.08331299

[View source]
def self.to_f64(str : String) : Float64 #

Converts a hexadecimal floating-point literal to a Float64.

The literal must match /\A-?0x[0-9A-Fa-f]+(\.[0-9A-Fa-f]+)?p([+-])?[0-9]+(_?f64)?\z/. Inexact literals are rounded to the nearest representable Float64, ties-to-even.

Does not support infinity and not-a-number.

HexFloat.to_f64("0x12.34p+5")      # => 582.5
HexFloat.to_f64("-0x0.555p-2_f64") # => -0.08331298828125

[View source]
def self.to_s(io : IO, num : Float64) : Nil #

Writes num to the given IO as a hexadecimal floating-point literal.


[View source]
def self.to_s(io : IO, num : Float32) : Nil #

Writes num to the given IO as a hexadecimal floating-point literal.


[View source]
def self.to_s(num : Float64) : String #

Returns the hexadecimal floating-point literal for the given num.

Returns Infinity for infinity, NaN for not-a-number.

HexFloat.to_s(6.125)    # => "0x1.88p+2"
HexFloat.to_s(-1.0 / 3) # => "-0x1.5555555555555p-2"

[View source]
def self.to_s(num : Float32) : String #

Returns the hexadecimal floating-point literal for the given num.

Returns Infinity for infinity, NaN for not-a-number.

HexFloat.to_s(1.111_f32)  # => "0x1.1c6a7ep+0"
HexFloat.to_s(-1_f32 / 3) # => "-0x1.555556p-2"

[View source]

Macro Detail

macro to_f(str) #

Converts a hexadecimal floating-point literal to a Float32 if it ends with f32, a Float64 otherwise.

str must be a string literal. The return type is never a union.

x = HexFloat.to_f("0x12.34p+5")     # => 582.5
x.class                             # => Float64
x = HexFloat.to_f("0x12.34p+5_f32") # => 582.5
x.class                             # => Float32

[View source]