abstract struct Number

Overview

The top-level number type.

Included Modules

Direct Known Subclasses

Defined in:

number.cr
yaml/to_yaml.cr
complex.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from module Comparable(T)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, between?(min, max) between?

Instance methods inherited from struct Value

! !, ==(other) ==, nil? nil?

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
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

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

def self.[](*nums) #

Creates an Array of self with the given values, which will be casted to this type with the new method (defined in each Number type).

floats = Float64[1, 2, 3, 4]
floats.class # => Array(Float64)

ints = Int64[1, 2, 3]
ints.class # => Array(Int64)

[View source]
def self.zero #

[View source]

Instance Method Detail

def *(other : Complex) #

[View source]
def + #

Returns self.


[View source]
def +(other : Complex) #

[View source]
def -(other : Complex) #

[View source]
def /(other : Complex) #

[View source]
def <=>(other) #

Implements the comparison operator.

See Object#<=>


[View source]
def ==(other : Complex) #

[View source]
def abs #

Returns the absolute value of this number.

123.abs  # => 123
-123.abs # => 123

[View source]
def abs2 #

Returns the square of self (self * self).

4.abs2   # => 16
1.5.abs2 # => 2.25

[View source]
def cis #

[View source]
def clamp(min, max) #

Clamps a value between min and max.

5.clamp(10, 100)   # => 10
50.clamp(10, 100)  # => 50
500.clamp(10, 100) # => 100

[View source]
def clamp(range : Range) #

Clamps a value within range.

5.clamp(10..100)   # => 10
50.clamp(10..100)  # => 50
500.clamp(10..100) # => 100

[View source]
def divmod(number) #

Returns a Tuple of two elements containing the quotient and modulus obtained by dividing self by number.

11.divmod(3)  # => {3, 2}
11.divmod(-3) # => {-3, 2}

[View source]
def i #

[View source]
def round(digits, base = 10) #

Rounds this number to a given precision in decimal digits.

-1763.116.round(2) # => -1763.12

[View source]
def sign #

Returns the sign of this number as an Int32.

  • -1 if this number is negative
  • 0 if this number is zero
  • 1 if this number is positive
123.sign # => 1
0.sign   # => 0
-42.sign # => -1

[View source]
def significant(digits, base = 10) #

Keeps digits significants digits of this number in the given base.

1234.567.significant(1) # => 1000
1234.567.significant(2) # => 1200
1234.567.significant(3) # => 1230
1234.567.significant(4) # => 1235
1234.567.significant(5) # => 1234.6
1234.567.significant(6) # => 1234.57
1234.567.significant(7) # => 1234.567
1234.567.significant(8) # => 1234.567

15.159.significant(1, base = 2) # => 16

[View source]
def step(limit = nil, by = 1) #

[View source]
def step(limit = nil, by = 1, &block) #

Invokes the given block with the sequence of numbers starting at self, incremented by by on each call, and with an optional limit.

3.step(by: 2, limit: 10) do |n|
  puts n
end

Output:

3
5
7
9

[View source]
def to_c #

[View source]
def to_yaml(yaml : YAML::Generator) #

[View source]