Float

abstract struct Float

Overview

Float is the base type of all floating point numbers.

There are two floating point types, Float32 and Float64, which correspond to the binary32 and binary64 types defined by IEEE.

A floating point literal is an optional + or #- sign, followed by a sequence of numbers or underscores, followed by a dot, followed by numbers or underscores, followed by an optional exponent suffix, followed by an optional type suffix. If no suffix is present, the literal's type is Float64.

1.0     # Float64
1.0_f32 # Float32
1_f32   # Float32

1e10   # Float64
1.5e10 # Float64
1.5e-7 # Float64

+1.3 # Float64
-0.5 # Float64

The underscore _ before the suffix is optional.

Underscores can be used to make some numbers more readable:

1_000_000.111_111 # better than 1000000.111111

Included Modules

Direct Known Subclasses

Defined in:

float.cr
time/span.cr
big/big_int.cr
big/big_rational.cr
json/to_json.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from module Comparable(BigRational)

<, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from module Comparable(BigInt)

<, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from struct Number

*(other : Complex)
*(other : BigFloat) *
, +(other : BigFloat)
+(other : Complex)
+ +
, -(other : Complex)
-(other : BigFloat) -
, /(other : Complex) /, <=>(other : BigFloat)
<=>(other) <=>
, ==(other : Complex) ==, abs abs, abs2 abs2, cis cis, clamp(min, max)
clamp(range : Range) clamp
, divmod(number) divmod, i i, round(digits, base = 10) round, sign sign, significant(digits, base = 10) significant, step(*, to = nil, by = 1, &block)
step(*, to = nil, by = 1) step
, to_big_f to_big_f, to_c to_c, to_yaml(yaml : YAML::Builder) to_yaml

Class methods inherited from struct Number

zero : self zero

Instance methods inherited from module Comparable(BigFloat)

<, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from module Comparable(Number)

<, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from struct Value

==(other) ==, dup dup

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===
, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json
, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json
, to_s
to_s(io : IO) to_s
, to_yaml(io : IO)
to_yaml to_yaml
, try(&block) try

Class methods inherited from class Object

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self from_json
, from_yaml(string_or_io) : self from_yaml

Class Method Detail

def self.from_io(io : IO, format : IO::ByteFormat)Source

Reads a float from the given io in the given format. See also: IO#read_bytes.

Instance Method Detail

def %(other)Source

def -Source

def <=>(other : BigRational)Source

def <=>(other : BigInt)Source

def daysSource

def fdiv(other)Source

def finite?Source

def hoursSource

def infinite?Source

def millisecondsSource

def minutesSource

def modulo(other)Source

def nan?Source

def remainder(other)Source

def secondsSource

def to_big_i : BigIntSource

Returns a BigInt representing this float (rounded using floor).

def to_io(io : IO, format : IO::ByteFormat)Source

Writes this float to the given io in the given format. See also: IO#write_bytes.

def to_json(json : JSON::Builder)Source

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/Float.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部