# abstract struct Number

- Number
- Value
- Object

## Overview

The top-level number type.

## Included Modules

- Comparable(BigFloat)
- Comparable(Number)

## Direct Known Subclasses

- BigDecimal
- BigRational
- Float
- Int

## Defined in:

## Constructors

- .zero : self

## Instance Method Summary

- #*(other : Complex)
- #*(other : BigFloat)
- #+(other : BigFloat)
- #+(other : Complex)
- #+

  Returns self.

- #-(other : Complex)
- #-(other : BigFloat)
- #/(other : Complex)
- #/(other : BigFloat)
- #<=>(other : BigFloat)
- #<=>(other)

  Implements the comparison operator.

- #==(other : Complex)
- #abs

  Returns the absolute value of this number.

- #abs2

  Returns the square of `self` (`self * self`).

- #cis
- #clamp(min, max)

  Clamps a value between min and max.

- #clamp(range : Range)

  Clamps a value within range.

- #divmod(number)

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

- #i
- #round(digits = 0, base = 10)

  Rounds this number to a given precision in decimal digits.

- #sign

  Returns the sign of this number as an `Int32`.

- #significant(digits, base = 10)

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

- #step(*, to = nil, by = 1)
- #step(*, to = 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 to.

- #to_big_f
- #to_c
- #to_yaml(yaml : YAML::Nodes::Builder)
- #zero? : Bool

  Returns `true` if value is equal to zero.

## Macro Summary

- [](*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).

- slice(*nums, read_only = false)

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

- static_array(*nums)

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

### Instance methods inherited from module `Comparable(BigFloat)`

<(other : T) <, <=(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) >, >=(other : T) >=

### Instance methods inherited from struct `Value`

==(other : JSON::Any)
==(other : YAML::Any)
==(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(hasher)
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, unsafe_as(type : T.class) forall T unsafe_as

### Constructor 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 : String | IO) : self from_yaml

## Constructor Detail

def self.zero : self #

## Instance Method Detail

def *(other : Complex) #
def *(other : BigFloat) #
def +(other : BigFloat) #
def +(other : Complex) #
def + #

Returns self.

def -(other : Complex) #
def -(other : BigFloat) #
def /(other : Complex) #
def /(other : BigFloat) #
def <=>(other : BigFloat) #
def <=>(other) #

Implements the comparison operator.

See also: `Object#<=>`.

def ==(other : Complex) #
def abs #

Returns the absolute value of this number.

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

def abs2 #

Returns the square of `self` (`self * self`).

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

def cis #
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

def clamp(range : Range) #

Clamps a value within range.

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

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) # => {-4, -1}

def i #
def round(digits = 0, base = 10) #

Rounds this number to a given precision in decimal digits.

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

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

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

def step(*, to = nil, by = 1) #
def step(*, to = 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 to.

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

Output:

    3
    5
    7
    9

def to_big_f #
def to_c #
def to_yaml(yaml : YAML::Nodes::Builder) #
def zero? : Bool #

Returns `true` if value is equal to zero.

    0.zero? # => true
    5.zero? # => false

## Macro Detail

macro [](*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)

macro slice(*nums, read_only = false) #

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

The slice is allocated on the heap.

    floats = Float64.slice(1, 2, 3, 4)
    floats.class # => Slice(Float64)

    ints = Int64.slice(1, 2, 3)
    ints.class # => Slice(Int64)

macro static_array(*nums) #

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

    floats = Float64.static_array(1, 2, 3, 4)
    floats.class # => StaticArray(Float64, 4)

    ints = Int64.static_array(1, 2, 3)
    ints.class # => StaticArray(Int64, 3)