diff --git a/src/big/big_decimal.cr b/src/big/big_decimal.cr index 413c0f0eb057..08d24def04d6 100644 --- a/src/big/big_decimal.cr +++ b/src/big/big_decimal.cr @@ -161,7 +161,7 @@ struct BigDecimal < Number end end - def +(other : Int) + def +(other : Int) : BigDecimal self + BigDecimal.new(other) end @@ -177,7 +177,7 @@ struct BigDecimal < Number end end - def -(other : Int) + def -(other : Int) : BigDecimal self - BigDecimal.new(other) end @@ -185,7 +185,7 @@ struct BigDecimal < Number BigDecimal.new(@value * other.value, @scale + other.scale) end - def *(other : Int) + def *(other : Int) : BigDecimal self * BigDecimal.new(other) end @@ -338,7 +338,7 @@ struct BigDecimal < Number end # Converts to `BigInt`. Truncates anything on the right side of the decimal point. - def to_big_i + def to_big_i : BigInt trunc.value end @@ -351,37 +351,37 @@ struct BigDecimal < Number self end - def to_big_r + def to_big_r : BigRational BigRational.new(self.value, BigDecimal::TEN ** self.scale) end # Converts to `Int64`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_i64 + def to_i64 : Int64 to_big_i.to_i64 end # Converts to `Int32`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_i32 + def to_i32 : Int32 to_big_i.to_i32 end # Converts to `Int16`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_i16 + def to_i16 : Int16 to_big_i.to_i16 end # Converts to `Int8`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_i8 + def to_i8 : Int8 to_big_i.to_i8 end # Converts to `Int32`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_i + def to_i : Int32 to_i32 end @@ -399,7 +399,7 @@ struct BigDecimal < Number # Converts to `Int32`. Truncates anything on the right side of the decimal point. # In case of overflow a wrapping is performed. - def to_i32! + def to_i32! : Int32 to_big_i.to_i32! end @@ -411,7 +411,7 @@ struct BigDecimal < Number # Converts to `Int32`. Truncates anything on the right side of the decimal point. # In case of overflow a wrapping is performed. - def to_i! + def to_i! : Int32 to_i32! end @@ -426,31 +426,31 @@ struct BigDecimal < Number # Converts to `UInt64`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_u64 + def to_u64 : UInt64 to_big_u.to_u64 end # Converts to `UInt32`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_u32 + def to_u32 : UInt32 to_big_u.to_u32 end # Converts to `UInt16`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_u16 + def to_u16 : UInt16 to_big_u.to_u16 end # Converts to `UInt8`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_u8 + def to_u8 : UInt8 to_big_u.to_u8 end # Converts to `UInt32`. Truncates anything on the right side of the decimal point. # Raises `OverflowError` in case of overflow. - def to_u + def to_u : UInt32 to_u32 end @@ -471,7 +471,7 @@ struct BigDecimal < Number # Converts to `UInt32`. Truncates anything on the right side of the decimal point, # converting negative to positive. # In case of overflow a wrapping is performed. - def to_u32! + def to_u32! : UInt32 to_big_u!.to_u32! end @@ -485,25 +485,25 @@ struct BigDecimal < Number # Converts to `UInt32`. Truncates anything on the right side of the decimal point, # converting negative to positive. # In case of overflow a wrapping is performed. - def to_u! + def to_u! : UInt32 to_u32! end # Converts to `Float64`. # Raises `OverflowError` in case of overflow. - def to_f64 + def to_f64 : Float64 to_s.to_f64 end # Converts to `Float32`. # Raises `OverflowError` in case of overflow. - def to_f32 + def to_f32 : Float32 to_f64.to_f32 end # Converts to `Float64`. # Raises `OverflowError` in case of overflow. - def to_f + def to_f : Float64 to_f64 end @@ -515,13 +515,13 @@ struct BigDecimal < Number # Converts to `Float64`. # In case of overflow a wrapping is performed. - def to_f64! + def to_f64! : Float64 to_f64 end # Converts to `Float64`. # In case of overflow a wrapping is performed. - def to_f! + def to_f! : Float64 to_f64! end @@ -572,7 +572,7 @@ struct Int # require "big" # 12123415151254124124.to_big_d # ``` - def to_big_d + def to_big_d : BigDecimal BigDecimal.new(self) end @@ -580,15 +580,15 @@ struct Int to_big_d <=> other end - def +(other : BigDecimal) + def +(other : BigDecimal) : BigDecimal other + self end - def -(other : BigDecimal) + def -(other : BigDecimal) : BigDecimal to_big_d - other end - def *(other : BigDecimal) + def *(other : BigDecimal) : BigDecimal other * self end end @@ -608,7 +608,7 @@ struct Float # require "big" # 1212341515125412412412421.0.to_big_d # ``` - def to_big_d + def to_big_d : BigDecimal BigDecimal.new(self) end end @@ -621,7 +621,7 @@ struct BigRational end # Converts `self` to `BigDecimal`. - def to_big_d + def to_big_d : BigDecimal BigDecimal.new(self) end end @@ -632,7 +632,7 @@ class String # require "big" # "1212341515125412412412421".to_big_d # ``` - def to_big_d + def to_big_d : BigDecimal BigDecimal.new(self) end end diff --git a/src/big/big_float.cr b/src/big/big_float.cr index 2a29b6efbe4b..aa2368570cac 100644 --- a/src/big/big_float.cr +++ b/src/big/big_float.cr @@ -115,23 +115,23 @@ struct BigFloat < Float end end - def - + def - : BigFloat BigFloat.new { |mpf| LibGMP.mpf_neg(mpf, self) } end - def +(other : Number) + def +(other : Number) : BigFloat BigFloat.new { |mpf| LibGMP.mpf_add(mpf, self, other.to_big_f) } end - def -(other : Number) + def -(other : Number) : BigFloat BigFloat.new { |mpf| LibGMP.mpf_sub(mpf, self, other.to_big_f) } end - def *(other : Number) + def *(other : Number) : BigFloat BigFloat.new { |mpf| LibGMP.mpf_mul(mpf, self, other.to_big_f) } end - def /(other : BigFloat) + def /(other : BigFloat) : BigFloat # Division by 0 in BigFloat is not allowed, there is no BigFloat::Infinity raise DivisionByZeroError.new if other == 0 BigFloat.new { |mpf| LibGMP.mpf_div(mpf, self, other) } @@ -141,55 +141,55 @@ struct BigFloat < Float Number.expand_div [BigDecimal], BigDecimal Number.expand_div [BigRational], BigRational - def **(other : Int) + def **(other : Int) : BigFloat BigFloat.new { |mpf| LibGMP.mpf_pow_ui(mpf, self, other.to_u64) } end - def abs + def abs : BigFloat BigFloat.new { |mpf| LibGMP.mpf_abs(mpf, self) } end - def ceil + def ceil : BigFloat BigFloat.new { |mpf| LibGMP.mpf_ceil(mpf, self) } end - def floor + def floor : BigFloat BigFloat.new { |mpf| LibGMP.mpf_floor(mpf, self) } end - def trunc + def trunc : BigFloat BigFloat.new { |mpf| LibGMP.mpf_trunc(mpf, self) } end - def to_f64 + def to_f64 : Float64 LibGMP.mpf_get_d(self) end - def to_f32 + def to_f32 : Float32 to_f64.to_f32 end - def to_f + def to_f : Float64 to_f64 end - def to_f32! + def to_f32! : Float32 to_f64.to_f32! end - def to_f64! + def to_f64! : Float64 to_f64 end - def to_f! + def to_f! : Float64 to_f64! end - def to_big_f + def to_big_f : BigFloat self end - def to_big_i + def to_big_i : BigInt BigInt.new { |mpz| LibGMP.set_f(mpz, mpf) } end @@ -197,23 +197,23 @@ struct BigFloat < Float LibGMP.mpf_get_si(self) end - def to_i32 + def to_i32 : Int32 to_i64.to_i32 end - def to_i16 + def to_i16 : Int16 to_i64.to_i16 end - def to_i8 + def to_i8 : Int8 to_i64.to_i8 end - def to_i + def to_i : Int32 to_i32 end - def to_i! + def to_i! : Int32 to_i32! end @@ -225,7 +225,7 @@ struct BigFloat < Float LibGMP.mpf_get_si(self).to_i16! end - def to_i32! + def to_i32! : Int32 LibGMP.mpf_get_si(self).to_i32! end @@ -238,23 +238,23 @@ struct BigFloat < Float LibGMP.mpf_get_ui(self) end - def to_u32 + def to_u32 : UInt32 to_u64.to_u32 end - def to_u16 + def to_u16 : UInt16 to_u64.to_u16 end - def to_u8 + def to_u8 : UInt8 to_u64.to_u8 end - def to_u + def to_u : UInt32 to_u32 end - def to_u! + def to_u! : UInt32 to_u32! end @@ -266,7 +266,7 @@ struct BigFloat < Float LibGMP.mpf_get_ui(self).to_u16! end - def to_u32! + def to_u32! : UInt32 LibGMP.mpf_get_ui(self).to_u32! end @@ -366,15 +366,15 @@ struct Number to_big_f - other end - def *(other : BigFloat) + def *(other : BigFloat) : BigFloat other * self end - def /(other : BigFloat) + def /(other : BigFloat) : BigFloat to_big_f / other end - def to_big_f + def to_big_f : BigFloat BigFloat.new(self) end end @@ -386,14 +386,14 @@ class String # require "big" # "1234.0".to_big_f # ``` - def to_big_f + def to_big_f : BigFloat BigFloat.new(self) end end module Math # Decomposes the given floating-point *value* into a normalized fraction and an integral power of two. - def frexp(value : BigFloat) + def frexp(value : BigFloat) : {BigFloat, Int32 | Int64} LibGMP.mpf_get_d_2exp(out exp, value) # we need BigFloat frac, so will skip Float64 one. frac = BigFloat.new do |mpf| if exp >= 0 @@ -412,7 +412,7 @@ module Math # # Math.sqrt(1_000_000_000_000.to_big_f * 1_000_000_000_000.to_big_f) # => 1000000000000.0 # ``` - def sqrt(value : BigFloat) + def sqrt(value : BigFloat) : BigFloat BigFloat.new { |mpf| LibGMP.mpf_sqrt(mpf, value) } end end diff --git a/src/big/big_int.cr b/src/big/big_int.cr index 9c50d55e7265..33dc1af402db 100644 --- a/src/big/big_int.cr +++ b/src/big/big_int.cr @@ -265,7 +265,7 @@ struct BigInt < Int unsafe_truncated_mod(other) end - def divmod(number : BigInt) + def divmod(number : BigInt) : {BigInt, BigInt} check_division_by_zero number unsafe_floored_divmod(number) @@ -276,7 +276,7 @@ struct BigInt < Int unsafe_floored_divmod(number) end - def divmod(number : Int::Signed) + def divmod(number : Int::Signed) : {BigInt, BigInt} check_division_by_zero number if number > 0 && number <= LibC::Long::MAX unsafe_floored_divmod(LibGMP::ULong.new(number)) @@ -320,13 +320,13 @@ struct BigInt < Int BigInt.new { |mpz| LibGMP.tdiv_r_ui(mpz, self, other.abs.to_big_i) } end - def unsafe_floored_divmod(number : BigInt) + def unsafe_floored_divmod(number : BigInt) : {BigInt, BigInt} the_q = BigInt.new the_r = BigInt.new { |r| LibGMP.fdiv_qr(the_q, r, self, number) } {the_q, the_r} end - def unsafe_floored_divmod(number : LibGMP::ULong) + def unsafe_floored_divmod(number : LibGMP::ULong) : {BigInt, BigInt} the_q = BigInt.new the_r = BigInt.new { |r| LibGMP.fdiv_qr_ui(the_q, r, self, number) } {the_q, the_r} @@ -454,31 +454,31 @@ struct BigInt < Int ary end - def popcount + def popcount : Int LibGMP.popcount(self) end - def trailing_zeros_count + def trailing_zeros_count : Int LibGMP.scan1(self, 0) end - def to_i + def to_i : Int32 to_i32 end - def to_i8 + def to_i8 : Int8 to_i32.to_i8 end - def to_i16 + def to_i16 : Int16 to_i32.to_i16 end - def to_i32 + def to_i32 : Int32 LibGMP.get_si(self).to_i32 end - def to_i64 + def to_i64 : Int64 if LibGMP::Long::MIN <= self <= LibGMP::Long::MAX LibGMP.get_si(self).to_i64 else @@ -490,39 +490,39 @@ struct BigInt < Int to_i32! end - def to_i8! + def to_i8! : Int8 LibGMP.get_si(self).to_i8! end - def to_i16! + def to_i16! : Int16 LibGMP.get_si(self).to_i16! end - def to_i32! + def to_i32! : Int32 LibGMP.get_si(self).to_i32! end - def to_i64! + def to_i64! : Int64 (self % BITS64).to_u64.to_i64! end - def to_u + def to_u : UInt32 to_u32 end - def to_u8 + def to_u8 : UInt8 to_u32.to_u8 end - def to_u16 + def to_u16 : UInt16 to_u32.to_u16 end - def to_u32 + def to_u32 : UInt32 to_u64.to_u32 end - def to_u64 + def to_u64 : UInt64 if LibGMP::ULong::MIN <= self <= LibGMP::ULong::MAX LibGMP.get_ui(self).to_u64 else @@ -534,33 +534,33 @@ struct BigInt < Int to_u32! end - def to_u8! + def to_u8! : UInt8 LibGMP.get_ui(self).to_u8! end - def to_u16! + def to_u16! : UInt16 LibGMP.get_ui(self).to_u16! end - def to_u32! + def to_u32! : UInt32 LibGMP.get_ui(self).to_u32! end - def to_u64! + def to_u64! : UInt64 (self % BITS64).to_u64 end private BITS64 = BigInt.new(1) << 64 - def to_f + def to_f : Float64 to_f64 end - def to_f32 + def to_f32 : Float32 to_f64.to_f32 end - def to_f64 + def to_f64 : Float64 LibGMP.get_d(self) end @@ -576,19 +576,19 @@ struct BigInt < Int LibGMP.get_d(self) end - def to_big_i + def to_big_i : BigInt self end - def to_big_f + def to_big_f : BigFloat BigFloat.new { |mpf| LibGMP.mpf_set_z(mpf, mpz) } end - def to_big_d + def to_big_d : BigDecimal BigDecimal.new(self) end - def to_big_r + def to_big_r : BigRational BigRational.new(self) end @@ -720,7 +720,7 @@ module Math # # Math.sqrt(1_000_000_000_000.to_big_i * 1_000_000_000_000.to_big_i) # => 1000000000000.0 # ``` - def sqrt(value : BigInt) + def sqrt(value : BigInt) : BigFloat sqrt(value.to_big_f) end end diff --git a/src/big/big_rational.cr b/src/big/big_rational.cr index 736478f2ac8b..b3c1d32d053c 100644 --- a/src/big/big_rational.cr +++ b/src/big/big_rational.cr @@ -80,11 +80,11 @@ struct BigRational < Number new(mpq) end - def numerator + def numerator : BigInt BigInt.new { |mpz| LibGMP.mpq_get_num(mpz, self) } end - def denominator + def denominator : BigInt BigInt.new { |mpz| LibGMP.mpq_get_den(mpz, self) } end @@ -104,47 +104,47 @@ struct BigRational < Number LibGMP.mpq_cmp(mpq, other.to_big_r) end - def +(other : BigRational) + def +(other : BigRational) : BigRational BigRational.new { |mpq| LibGMP.mpq_add(mpq, self, other) } end - def +(other : Int) + def +(other : Int) : BigRational self + other.to_big_r end - def -(other : BigRational) + def -(other : BigRational) : BigRational BigRational.new { |mpq| LibGMP.mpq_sub(mpq, self, other) } end - def -(other : Int) + def -(other : Int) : BigRational self - other.to_big_r end - def *(other : BigRational) + def *(other : BigRational) : BigRational BigRational.new { |mpq| LibGMP.mpq_mul(mpq, self, other) } end - def *(other : Int) + def *(other : Int) : BigRational self * other.to_big_r end - def /(other : BigRational) + def /(other : BigRational) : BigRational check_division_by_zero other BigRational.new { |mpq| LibGMP.mpq_div(mpq, self, other) } end Number.expand_div [BigInt, BigFloat, BigDecimal], BigRational - def ceil + def ceil : BigRational diff = (denominator - numerator % denominator) % denominator BigRational.new(numerator + diff, denominator) end - def floor + def floor : BigRational BigRational.new(numerator - numerator % denominator, denominator) end - def trunc + def trunc : BigRational self < 0 ? ceil : floor end @@ -155,7 +155,7 @@ struct BigRational < Number # # BigRational.new(2, 3) >> 2 # => 1/6 # ``` - def >>(other : Int) + def >>(other : Int) : BigRational BigRational.new { |mpq| LibGMP.mpq_div_2exp(mpq, self, other) } end @@ -166,11 +166,11 @@ struct BigRational < Number # # BigRational.new(2, 3) << 2 # => 8/3 # ``` - def <<(other : Int) + def <<(other : Int) : BigRational BigRational.new { |mpq| LibGMP.mpq_mul_2exp(mpq, self, other) } end - def - + def - : BigRational BigRational.new { |mpq| LibGMP.mpq_neg(mpq, self) } end @@ -194,12 +194,12 @@ struct BigRational < Number # Returns a new `BigRational` as 1/r. # # This will raise an exception if rational is 0. - def inv + def inv : BigRational check_division_by_zero self BigRational.new { |mpq| LibGMP.mpq_inv(mpq, self) } end - def abs + def abs : BigRational BigRational.new { |mpq| LibGMP.mpq_abs(mpq, self) } end @@ -207,37 +207,37 @@ struct BigRational < Number def_hash to_f64 # Returns the `Float64` representing this rational. - def to_f + def to_f : Float64 to_f64 end - def to_f32 + def to_f32 : Float32 to_f64.to_f32 end - def to_f64 + def to_f64 : Float64 LibGMP.mpq_get_d(mpq) end - def to_f32! + def to_f32! : Float32 to_f64.to_f32! end - def to_f64! + def to_f64! : Float64 to_f64 end - def to_f! + def to_f! : Float64 to_f64! end delegate to_i8, to_i16, to_i32, to_i64, to_u8, to_u16, to_u32, to_u64, to: to_f64 - def to_big_f + def to_big_f : BigFloat BigFloat.new { |mpf| LibGMP.mpf_set_q(mpf, mpq) } end - def to_big_i + def to_big_i : BigInt BigInt.new { |mpz| LibGMP.set_q(mpz, mpq) } end @@ -301,7 +301,7 @@ struct Int # # 123.to_big_r # ``` - def to_big_r + def to_big_r : BigRational BigRational.new(self, 1) end @@ -309,11 +309,11 @@ struct Int -(other <=> self) end - def +(other : BigRational) + def +(other : BigRational) : BigRational other + self end - def -(other : BigRational) + def -(other : BigRational) : BigRational self.to_big_r - other end @@ -321,7 +321,7 @@ struct Int self.to_big_r / other end - def *(other : BigRational) + def *(other : BigRational) : BigRational other * self end end @@ -335,7 +335,7 @@ struct Float # # 123.0.to_big_r # ``` - def to_big_r + def to_big_r : BigRational BigRational.new(self) end @@ -352,7 +352,7 @@ module Math # # Math.sqrt(1_000_000_000_000.to_big_r * 1_000_000_000_000.to_big_r) # => 1000000000000.0 # ``` - def sqrt(value : BigRational) + def sqrt(value : BigRational) : BigFloat sqrt(value.to_big_f) end end diff --git a/src/big/number.cr b/src/big/number.cr index f53df0521950..8cdacf5413e5 100644 --- a/src/big/number.cr +++ b/src/big/number.cr @@ -8,7 +8,7 @@ struct BigFloat self.class.new(self / other) end - def /(other : UInt8 | UInt16 | UInt32 | UInt64) + def /(other : UInt8 | UInt16 | UInt32 | UInt64) : BigFloat # Division by 0 in BigFloat is not allowed, there is no BigFloat::Infinity raise DivisionByZeroError.new if other == 0 if other.is_a?(UInt8 | UInt16 | UInt32) || (LibGMP::ULong == UInt64 && other.is_a?(UInt64)) diff --git a/src/complex.cr b/src/complex.cr index 654729e7b7b8..4b6482e9efad 100644 --- a/src/complex.cr +++ b/src/complex.cr @@ -50,7 +50,7 @@ struct Complex # Returns the value as a `Float64` if possible (the imaginary part should be exactly zero), # raises otherwise. - def to_f64 + def to_f64 : Float64 unless @imag.zero? raise Exception.new "Complex number with non-zero imaginary part can't be converted to real number" end @@ -59,7 +59,7 @@ struct Complex # Returns the value as a `Float32` if possible (the imaginary part should be exactly zero), # raises otherwise. - def to_f32 + def to_f32 : Float32 to_f64.to_f32 end @@ -70,7 +70,7 @@ struct Complex # Returns the value as an `Int64` if possible (the imaginary part should be exactly zero), # raises otherwise. - def to_i64 + def to_i64 : Int64 to_f64.to_i64 end @@ -78,7 +78,7 @@ struct Complex # Returns the value as an `UInt64` if possible (the imaginary part should be exactly zero), # raises otherwise. - def to_u64 + def to_u64 : UInt64 to_f64.to_u64 end @@ -125,7 +125,7 @@ struct Complex # Complex.new(42, 2).abs # => 42.04759208325728 # Complex.new(-42, 2).abs # => 42.04759208325728 # ``` - def abs + def abs : Float64 Math.hypot(@real, @imag) end @@ -136,16 +136,16 @@ struct Complex # # Complex.new(42, 2).abs2 # => 1768 # ``` - def abs2 + def abs2 : Float64 @real * @real + @imag * @imag end - def sign + def sign : Complex self / abs end # Returns the phase of `self`. - def phase + def phase : Float64 Math.atan2(@imag, @real) end @@ -156,7 +156,7 @@ struct Complex # # Complex.new(42, 2).polar # => {42.047592083257278, 0.047583103276983396} # ``` - def polar + def polar : {Float64, Float64} {abs, phase} end @@ -168,57 +168,57 @@ struct Complex # Complex.new(42, 2).conj # => 42.0 - 2.0i # Complex.new(42, -2).conj # => 42.0 + 2.0i # ``` - def conj + def conj : Complex Complex.new(@real, -@imag) end # Returns the inverse of `self`. - def inv + def inv : Complex conj / abs2 end # Returns `self`. - def + + def + : Complex self end # Adds the value of `self` to *other*. - def +(other : Complex) + def +(other : Complex) : Complex Complex.new(@real + other.real, @imag + other.imag) end # :ditto: - def +(other : Number) + def +(other : Number) : Complex Complex.new(@real + other, @imag) end # Returns the opposite of `self`. - def - + def - : Complex Complex.new(-@real, -@imag) end # Removes the value of *other* from `self`. - def -(other : Complex) + def -(other : Complex) : Complex Complex.new(@real - other.real, @imag - other.imag) end # :ditto: - def -(other : Number) + def -(other : Number) : Complex Complex.new(@real - other, @imag) end # Multiplies `self` by *other*. - def *(other : Complex) + def *(other : Complex) : Complex Complex.new(@real * other.real - @imag * other.imag, @real * other.imag + @imag * other.real) end # :ditto: - def *(other : Number) + def *(other : Number) : Complex Complex.new(@real * other, @imag * other) end # Divides `self` by *other*. - def /(other : Complex) + def /(other : Complex) : Complex if other.real <= other.imag r = other.real / other.imag d = other.imag + r * other.real @@ -231,7 +231,7 @@ struct Complex end # :ditto: - def /(other : Number) + def /(other : Number) : Complex Complex.new(@real / other, @imag / other) end @@ -264,17 +264,17 @@ struct Complex end # Rounds to the nearest *digits*. - def round(digits = 0) + def round(digits = 0) : Complex Complex.new(@real.round(digits), @imag.round(digits)) end end struct Number - def to_c + def to_c : Complex Complex.new(self, 0) end - def i + def i : Complex Complex.new(0, self) end @@ -282,23 +282,23 @@ struct Number to_c == other end - def cis + def cis : Complex Complex.new(Math.cos(self), Math.sin(self)) end - def +(other : Complex) + def +(other : Complex) : Complex Complex.new(self + other.real, other.imag) end - def -(other : Complex) + def -(other : Complex) : Complex Complex.new(self - other.real, -other.imag) end - def *(other : Complex) + def *(other : Complex) : Complex Complex.new(self * other.real, self * other.imag) end - def /(other : Complex) + def /(other : Complex) : Complex self * other.inv end end @@ -311,7 +311,7 @@ module Math # # Math.exp(4 + 2.i) # => -22.720847417619233 + 49.645957334580565i # ``` - def exp(value : Complex) + def exp(value : Complex) : Complex r = exp(value.real) Complex.new(r * cos(value.imag), r * sin(value.imag)) end @@ -323,7 +323,7 @@ module Math # # Math.log(4 + 2.i) # => 1.4978661367769956 + 0.4636476090008061i # ``` - def log(value : Complex) + def log(value : Complex) : Complex Complex.new(Math.log(value.abs), value.phase) end @@ -334,7 +334,7 @@ module Math # # Math.log2(4 + 2.i) # => 2.1609640474436813 + 0.6689021062254881i # ``` - def log2(value : Complex) + def log2(value : Complex) : Complex log(value) / LOG2 end @@ -345,7 +345,7 @@ module Math # # Math.log10(4 + 2.i) # => 0.6505149978319906 + 0.20135959813668655i # ``` - def log10(value : Complex) + def log10(value : Complex) : Complex log(value) / LOG10 end @@ -367,7 +367,7 @@ module Math # Math.sqrt(-1.0) # => -NaN # Math.sqrt(-1.0 + 0.0.i) # => 0.0 + 1.0i # ``` - def sqrt(value : Complex) + def sqrt(value : Complex) : Complex r = value.abs re = if value.real >= 0 diff --git a/src/float.cr b/src/float.cr index 20854b9e9519..483c8027b3dd 100644 --- a/src/float.cr +++ b/src/float.cr @@ -49,11 +49,11 @@ struct Float modulo(other) end - def nan? + def nan? : Bool !(self == self) end - def infinite? + def infinite? : Int32? if nan? || self == 0 || self != 2 * self nil else @@ -61,7 +61,7 @@ struct Float end end - def finite? + def finite? : Bool !nan? && !infinite? end @@ -146,24 +146,24 @@ struct Float32 end # Returns a `Float32` by invoking `to_f32!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_f32! end Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], Float32 Number.expand_div [Float64], Float64 - def ceil + def ceil : Float32 LibM.ceil_f32(self) end - def floor + def floor : Float32 LibM.floor_f32(self) end # Rounds towards the nearest integer. If both neighboring integers are equidistant, # rounds towards the even neighbor (Banker's rounding). - def round_even : self + def round_even : Float32 # TODO: LLVM 11 introduced llvm.roundeven.* intrinsics which may replace # rint in the future. LibM.rint_f32(self) @@ -171,11 +171,11 @@ struct Float32 # Rounds towards the nearest integer. If both neighboring integers are equidistant, # rounds away from zero. - def round_away + def round_away : Float32 LibM.round_f32(self) end - def trunc + def trunc : Float32 LibM.trunc_f32(self) end @@ -187,11 +187,11 @@ struct Float32 {% end %} end - def **(other : Float32) + def **(other : Float32) : Float32 LibM.pow_f32(self, other) end - def **(other) + def **(other) : Float32 self ** other.to_f32 end @@ -253,24 +253,24 @@ struct Float64 end # Returns a `Float64` by invoking `to_f64!` on *value*. - def Float64.new!(value) + def Float64.new!(value) : Float64 value.to_f64! end Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], Float64 Number.expand_div [Float32], Float64 - def ceil + def ceil : Float64 LibM.ceil_f64(self) end - def floor + def floor : Float64 LibM.floor_f64(self) end # Rounds towards the nearest integer. If both neighboring integers are equidistant, # rounds towards the even neighbor (Banker's rounding). - def round_even : self + def round_even : Float64 # TODO: LLVM 11 introduced llvm.roundeven.* intrinsics which may replace # rint in the future. LibM.rint_f64(self) @@ -278,11 +278,11 @@ struct Float64 # Rounds towards the nearest integer. If both neighboring integers are equidistant, # rounds away from zero. - def round_away + def round_away : Float64 LibM.round_f64(self) end - def trunc + def trunc : Float64 LibM.trunc_f64(self) end @@ -294,11 +294,11 @@ struct Float64 {% end %} end - def **(other : Float64) + def **(other : Float64) : Float64 LibM.pow_f64(self, other) end - def **(other) + def **(other) : Float64 self ** other.to_f64 end diff --git a/src/int.cr b/src/int.cr index a39d37583b09..d4a006ba5d3a 100644 --- a/src/int.cr +++ b/src/int.cr @@ -145,7 +145,7 @@ struct Int {% end %} end - def fdiv(other) + def fdiv(other) : Float64 to_f / other end @@ -237,7 +237,7 @@ struct Int self > other ? 1 : (self < other ? -1 : 0) end - def abs + def abs : self self >= 0 ? self : -self end @@ -245,15 +245,15 @@ struct Int self end - def ceil + def ceil : self self end - def floor + def floor : self self end - def trunc + def trunc : self self end @@ -492,15 +492,15 @@ struct Int (self // gcd(other) * other).abs end - def divisible_by?(num) + def divisible_by?(num) : Bool remainder(num) == 0 end - def even? + def even? : Bool divisible_by? 2 end - def odd? + def odd? : Bool !even? end @@ -509,11 +509,11 @@ struct Int hasher.int(self) end - def succ + def succ : self self + 1 end - def pred + def pred : self self - 1 end @@ -779,12 +779,12 @@ struct Int8 end # Returns an `Int8` by invoking `to_i8` on *value*. - def self.new(value) + def self.new(value) : self value.to_i8 end # Returns an `Int8` by invoking `to_i8!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_i8! end @@ -792,11 +792,11 @@ struct Int8 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def - + def - : Int8 0_i8 - self end - def popcount + def popcount : Int8 Intrinsics.popcount8(self) end @@ -830,12 +830,12 @@ struct Int16 end # Returns an `Int16` by invoking `to_i16` on *value*. - def self.new(value) + def self.new(value) : self value.to_i16 end # Returns an `Int16` by invoking `to_i16!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_i16! end @@ -843,11 +843,11 @@ struct Int16 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def - + def - : Int16 0_i16 - self end - def popcount + def popcount : Int16 Intrinsics.popcount16(self) end @@ -881,12 +881,12 @@ struct Int32 end # Returns an `Int32` by invoking `to_i32` on *value*. - def self.new(value) + def self.new(value) : self value.to_i32 end # Returns an `Int32` by invoking `to_i32!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_i32! end @@ -894,11 +894,11 @@ struct Int32 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def - + def - : Int32 0 - self end - def popcount + def popcount : Int32 Intrinsics.popcount32(self) end @@ -932,12 +932,12 @@ struct Int64 end # Returns an `Int64` by invoking `to_i64` on *value*. - def self.new(value) + def self.new(value) : self value.to_i64 end # Returns an `Int64` by invoking `to_i64!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_i64! end @@ -945,11 +945,11 @@ struct Int64 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def - + def - : Int64 0_i64 - self end - def popcount + def popcount : Int64 Intrinsics.popcount64(self) end @@ -984,12 +984,12 @@ struct Int128 end # Returns an `Int128` by invoking `to_i128` on *value*. - def self.new(value) + def self.new(value) : self value.to_i128 end # Returns an `Int128` by invoking `to_i128!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_i128! end @@ -1036,12 +1036,12 @@ struct UInt8 end # Returns an `UInt8` by invoking `to_u8` on *value*. - def self.new(value) + def self.new(value) : self value.to_u8 end # Returns an `UInt8` by invoking `to_u8!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_u8! end @@ -1049,15 +1049,15 @@ struct UInt8 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def &- + def &- : UInt8 0_u8 &- self end - def abs + def abs : self self end - def popcount + def popcount : Int8 Intrinsics.popcount8(self) end @@ -1091,12 +1091,12 @@ struct UInt16 end # Returns an `UInt16` by invoking `to_u16` on *value*. - def self.new(value) + def self.new(value) : self value.to_u16 end # Returns an `UInt16` by invoking `to_u16!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_u16! end @@ -1104,15 +1104,15 @@ struct UInt16 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def &- + def &- : UInt16 0_u16 &- self end - def abs + def abs : self self end - def popcount + def popcount : Int16 Intrinsics.popcount16(self) end @@ -1146,12 +1146,12 @@ struct UInt32 end # Returns an `UInt32` by invoking `to_u32` on *value*. - def self.new(value) + def self.new(value) : self value.to_u32 end # Returns an `UInt32` by invoking `to_u32!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_u32! end @@ -1159,15 +1159,15 @@ struct UInt32 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def &- + def &- : UInt32 0_u32 &- self end - def abs + def abs : self self end - def popcount + def popcount : Int32 Intrinsics.popcount32(self) end @@ -1201,12 +1201,12 @@ struct UInt64 end # Returns an `UInt64` by invoking `to_u64` on *value*. - def self.new(value) + def self.new(value) : self value.to_u64 end # Returns an `UInt64` by invoking `to_u64!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_u64! end @@ -1214,15 +1214,15 @@ struct UInt64 Number.expand_div [Float32], Float32 Number.expand_div [Float64], Float64 - def &- + def &- : UInt64 0_u64 &- self end - def abs + def abs : self self end - def popcount + def popcount : Int64 Intrinsics.popcount64(self) end @@ -1257,12 +1257,12 @@ struct UInt128 end # Returns an `UInt128` by invoking `to_u128` on *value*. - def self.new(value) + def self.new(value) : self value.to_u128 end # Returns an `UInt128` by invoking `to_u128!` on *value*. - def self.new!(value) + def self.new!(value) : self value.to_u128! end diff --git a/src/intrinsics.cr b/src/intrinsics.cr index cb5e6c36f041..45d686e4b4f0 100644 --- a/src/intrinsics.cr +++ b/src/intrinsics.cr @@ -93,7 +93,7 @@ module Intrinsics LibIntrinsics.read_cycle_counter end - def self.bswap32(id) + def self.bswap32(id) : UInt32 LibIntrinsics.bswap32(id) end @@ -101,19 +101,19 @@ module Intrinsics LibIntrinsics.bswap16(id) end - def self.popcount8(src) + def self.popcount8(src) : Int8 LibIntrinsics.popcount8(src) end - def self.popcount16(src) + def self.popcount16(src) : Int16 LibIntrinsics.popcount16(src) end - def self.popcount32(src) + def self.popcount32(src) : Int32 LibIntrinsics.popcount32(src) end - def self.popcount64(src) + def self.popcount64(src) : Int64 LibIntrinsics.popcount64(src) end diff --git a/src/math/math.cr b/src/math/math.cr index 01d03bb56b67..7946d29e075f 100644 --- a/src/math/math.cr +++ b/src/math/math.cr @@ -13,12 +13,12 @@ module Math LOG10 = LibM.log_f64(10.0) # Calculates the sine of *value*, measured in radians. - def sin(value : Float32) + def sin(value : Float32) : Float32 LibM.sin_f32(value) end # :ditto: - def sin(value : Float64) + def sin(value : Float64) : Float64 LibM.sin_f64(value) end @@ -28,12 +28,12 @@ module Math end # Calculates the cosine of *value*, measured in radians. - def cos(value : Float32) + def cos(value : Float32) : Float32 LibM.cos_f32(value) end # :ditto: - def cos(value : Float64) + def cos(value : Float64) : Float64 LibM.cos_f64(value) end @@ -43,12 +43,12 @@ module Math end # Calculates the tangent of *value*, measured in radians. - def tan(value : Float32) + def tan(value : Float32) : Float32 LibM.tan_f32(value) end # :ditto: - def tan(value : Float64) + def tan(value : Float64) : Float64 LibM.tan_f64(value) end @@ -58,12 +58,12 @@ module Math end # Calculates the arc sine of *value*. - def asin(value : Float32) + def asin(value : Float32) : Float32 LibM.asin_f32(value) end # :ditto: - def asin(value : Float64) + def asin(value : Float64) : Float64 LibM.asin_f64(value) end @@ -73,12 +73,12 @@ module Math end # Calculates the arc cosine of *value*. - def acos(value : Float32) + def acos(value : Float32) : Float32 LibM.acos_f32(value) end # :ditto: - def acos(value : Float64) + def acos(value : Float64) : Float64 LibM.acos_f64(value) end @@ -88,12 +88,12 @@ module Math end # Calculates the arc tangent of *value*. - def atan(value : Float32) + def atan(value : Float32) : Float32 LibM.atan_f32(value) end # :ditto: - def atan(value : Float64) + def atan(value : Float64) : Float64 LibM.atan_f64(value) end @@ -103,27 +103,27 @@ module Math end # Calculates the two-argument arc tangent of the ray from (0, 0) to (*x*, *y*). - def atan2(y : Float32, x : Float32) + def atan2(y : Float32, x : Float32) : Float32 LibM.atan2_f32(y, x) end # :ditto: - def atan2(y : Float64, x : Float64) + def atan2(y : Float64, x : Float64) : Float64 LibM.atan2_f64(y, x) end # :ditto: - def atan2(y, x) + def atan2(y, x) : Float64 atan2(y.to_f, x.to_f) end # Calculates the hyperbolic sine of *value*. - def sinh(value : Float32) + def sinh(value : Float32) : Float32 LibM.sinh_f32(value) end # :ditto: - def sinh(value : Float64) + def sinh(value : Float64) : Float64 LibM.sinh_f64(value) end @@ -133,12 +133,12 @@ module Math end # Calculates the hyperbolic cosine of *value*. - def cosh(value : Float32) + def cosh(value : Float32) : Float32 LibM.cosh_f32(value) end # :ditto: - def cosh(value : Float64) + def cosh(value : Float64) : Float64 LibM.cosh_f64(value) end @@ -148,12 +148,12 @@ module Math end # Calculates the hyperbolic tangent of *value*. - def tanh(value : Float32) + def tanh(value : Float32) : Float32 LibM.tanh_f32(value) end # :ditto: - def tanh(value : Float64) + def tanh(value : Float64) : Float64 LibM.tanh_f64(value) end @@ -163,12 +163,12 @@ module Math end # Calculates the inverse hyperbolic sine of *value*. - def asinh(value : Float32) + def asinh(value : Float32) : Float32 LibM.asinh_f32(value) end # :ditto: - def asinh(value : Float64) + def asinh(value : Float64) : Float64 LibM.asinh_f64(value) end @@ -178,12 +178,12 @@ module Math end # Calculates the inverse hyperbolic cosine of *value*. - def acosh(value : Float32) + def acosh(value : Float32) : Float32 LibM.acosh_f32(value) end # :ditto: - def acosh(value : Float64) + def acosh(value : Float64) : Float64 LibM.acosh_f64(value) end @@ -193,12 +193,12 @@ module Math end # Calculates the inverse hyperbolic tangent of *value*. - def atanh(value : Float32) + def atanh(value : Float32) : Float32 LibM.atanh_f32(value) end # :ditto: - def atanh(value : Float64) + def atanh(value : Float64) : Float64 LibM.atanh_f64(value) end @@ -208,12 +208,12 @@ module Math end # Calculates the exponential of *value*. - def exp(value : Float32) + def exp(value : Float32) : Float32 LibM.exp_f32(value) end # :ditto: - def exp(value : Float64) + def exp(value : Float64) : Float64 LibM.exp_f64(value) end @@ -223,12 +223,12 @@ module Math end # Calculates the exponential of *value*, minus 1. - def expm1(value : Float32) + def expm1(value : Float32) : Float32 LibM.expm1_f32(value) end # :ditto: - def expm1(value : Float64) + def expm1(value : Float64) : Float64 LibM.expm1_f64(value) end @@ -238,12 +238,12 @@ module Math end # Calculates 2 raised to the power *value*. - def exp2(value : Float32) + def exp2(value : Float32) : Float32 LibM.exp2_f32(value) end # :ditto: - def exp2(value : Float64) + def exp2(value : Float64) : Float64 LibM.exp2_f64(value) end @@ -253,27 +253,27 @@ module Math end # Calculates the natural logarithm of *value*. - def log(value : Float32) + def log(value : Float32) : Float32 LibM.log_f32(value) end # :ditto: - def log(value : Float64) + def log(value : Float64) : Float64 LibM.log_f64(value) end # :ditto: - def log(value) + def log(value) : Float64 log(value.to_f) end # Calculates the natural logarithm of 1 plus *value*. - def log1p(value : Float32) + def log1p(value : Float32) : Float32 LibM.log1p_f32(value) end # :ditto: - def log1p(value : Float64) + def log1p(value : Float64) : Float64 LibM.log1p_f64(value) end @@ -283,27 +283,27 @@ module Math end # Calculates the logarithm of *value* to base 2. - def log2(value : Float32) + def log2(value : Float32) : Float32 LibM.log2_f32(value) end # :ditto: - def log2(value : Float64) + def log2(value : Float64) : Float64 LibM.log2_f64(value) end # :ditto: - def log2(value) + def log2(value) : Float64 log2(value.to_f) end # Calculates the logarithm of *value* to base 10. - def log10(value : Float32) + def log10(value : Float32) : Float32 LibM.log10_f32(value) end # :ditto: - def log10(value : Float64) + def log10(value : Float64) : Float64 LibM.log10_f64(value) end @@ -318,27 +318,27 @@ module Math end # Calculates the square root of *value*. - def sqrt(value : Float32) + def sqrt(value : Float32) : Float32 LibM.sqrt_f32(value) end # :ditto: - def sqrt(value : Float64) + def sqrt(value : Float64) : Float64 LibM.sqrt_f64(value) end # :ditto: - def sqrt(value) + def sqrt(value) : Float64 sqrt(value.to_f) end # Calculates the cubic root of *value*. - def cbrt(value : Float32) + def cbrt(value : Float32) : Float32 LibM.cbrt_f32(value) end # :ditto: - def cbrt(value : Float64) + def cbrt(value : Float64) : Float64 LibM.cbrt_f64(value) end @@ -348,12 +348,12 @@ module Math end # Calculates the error function of *value*. - def erf(value : Float32) + def erf(value : Float32) : Float32 LibM.erf_f32(value) end # :ditto: - def erf(value : Float64) + def erf(value : Float64) : Float64 LibM.erf_f64(value) end @@ -363,12 +363,12 @@ module Math end # Calculates 1 minus the error function of *value*. - def erfc(value : Float32) + def erfc(value : Float32) : Float32 LibM.erfc_f32(value) end # :ditto: - def erfc(value : Float64) + def erfc(value : Float64) : Float64 LibM.erfc_f64(value) end @@ -381,17 +381,17 @@ module Math # # Note that `gamma(n)` is same as `fact(n - 1)` for integer `n > 0`. # However `gamma(n)` returns float and can be an approximation. - def gamma(value : Float32) + def gamma(value : Float32) : Float32 LibM.tgamma_f32(value) end # :ditto: - def gamma(value : Float64) + def gamma(value : Float64) : Float64 LibM.tgamma_f64(value) end # :ditto: - def gamma(value) + def gamma(value) : Float64 gamma(value.to_f) end @@ -413,12 +413,12 @@ module Math end # :ditto: - def lgamma(value : Float64) + def lgamma(value : Float64) : Float64 LibM.gamma_f64(value) end # :ditto: - def lgamma(value) + def lgamma(value) : Float64 lgamma(value.to_f) end @@ -432,7 +432,7 @@ module Math end # :ditto: - def besselj(order : Int32, value : Float64) + def besselj(order : Int32, value : Float64) : Float64 LibM.besselj_f64(order, value) end @@ -451,7 +451,7 @@ module Math end # :ditto: - def besselj0(value : Float64) + def besselj0(value : Float64) : Float64 LibM.besselj0_f64(value) end @@ -470,7 +470,7 @@ module Math end # :ditto: - def besselj1(value : Float64) + def besselj1(value : Float64) : Float64 LibM.besselj1_f64(value) end @@ -489,7 +489,7 @@ module Math end # :ditto: - def bessely(order : Int32, value : Float64) + def bessely(order : Int32, value : Float64) : Float64 LibM.bessely_f64(order, value) end @@ -508,7 +508,7 @@ module Math end # :ditto: - def bessely0(value : Float64) + def bessely0(value : Float64) : Float64 LibM.bessely0_f64(value) end @@ -527,7 +527,7 @@ module Math end # :ditto: - def bessely1(value : Float64) + def bessely1(value : Float64) : Float64 LibM.bessely1_f64(value) end @@ -542,12 +542,12 @@ module Math # ``` # Math.sqrt(value1 ** 2 + value2 ** 2) # ``` - def hypot(value1 : Float32, value2 : Float32) + def hypot(value1 : Float32, value2 : Float32) : Float32 LibM.hypot_f32(value1, value2) end # :ditto: - def hypot(value1 : Float64, value2 : Float64) + def hypot(value1 : Float64, value2 : Float64) : Float64 LibM.hypot_f64(value1, value2) end @@ -557,12 +557,12 @@ module Math end # Returns the unbiased base 2 exponent of the given floating-point *value*. - def ilogb(value : Float32) + def ilogb(value : Float32) : Int32 LibM.ilogb_f32(value) end # :ditto: - def ilogb(value : Float64) + def ilogb(value : Float64) : Int32 LibM.ilogb_f64(value) end @@ -574,12 +574,12 @@ module Math # Returns the unbiased radix-independent exponent of the given floating-point *value*. # # For `Float32` and `Float64` this is equivalent to `ilogb`. - def logb(value : Float32) + def logb(value : Float32) : Float32 LibM.logb_f32(value) end # :ditto: - def logb(value : Float64) + def logb(value : Float64) : Float64 LibM.logb_f64(value) end @@ -589,12 +589,12 @@ module Math end # Multiplies the given floating-point *value* by 2 raised to the power *exp*. - def ldexp(value : Float32, exp : Int32) + def ldexp(value : Float32, exp : Int32) : Float32 LibM.ldexp_f32(value, exp) end # :ditto: - def ldexp(value : Float64, exp : Int32) + def ldexp(value : Float64, exp : Int32) : Float64 LibM.ldexp_f64(value, exp) end @@ -606,12 +606,12 @@ module Math # Returns the floating-point *value* with its exponent raised by *exp*. # # For `Float32` and `Float64` this is equivalent to `ldexp`. - def scalbn(value : Float32, exp : Int32) + def scalbn(value : Float32, exp : Int32) : Float32 LibM.scalbn_f32(value, exp) end # :ditto: - def scalbn(value : Float64, exp : Int32) + def scalbn(value : Float64, exp : Int32) : Float64 LibM.scalbn_f64(value, exp) end @@ -626,23 +626,23 @@ module Math end # :ditto: - def scalbln(value : Float64, exp : Int64) + def scalbln(value : Float64, exp : Int64) : Float64 LibM.scalbln_f64(value, exp) end # :ditto: - def scalbln(value, exp) + def scalbln(value, exp) : Float64 scalbln(value.to_f, exp.to_i64) end # Decomposes the given floating-point *value* into a normalized fraction and an integral power of two. - def frexp(value : Float32) + def frexp(value : Float32) : {Float32, Int32} frac = LibM.frexp_f32(value, out exp) {frac, exp} end # :ditto: - def frexp(value : Float64) + def frexp(value : Float64) : {Float64, Int32} frac = LibM.frexp_f64(value, out exp) {frac, exp} end @@ -658,7 +658,7 @@ module Math end # :ditto: - def copysign(value1 : Float64, value2 : Float64) + def copysign(value1 : Float64, value2 : Float64) : Float64 LibM.copysign_f64(value1, value2) end @@ -673,7 +673,7 @@ module Math end # :ditto: - def max(value1 : Float64, value2 : Float64) + def max(value1 : Float64, value2 : Float64) : Float64 LibM.max_f64(value1, value2) end @@ -688,7 +688,7 @@ module Math end # :ditto: - def min(value1 : Float64, value2 : Float64) + def min(value1 : Float64, value2 : Float64) : Float64 LibM.min_f64(value1, value2) end diff --git a/src/number.cr b/src/number.cr index e0a3bb1b66bb..096e1f99fd77 100644 --- a/src/number.cr +++ b/src/number.cr @@ -146,7 +146,7 @@ struct Number # 123.abs # => 123 # -123.abs # => 123 # ``` - def abs + def abs : self self < 0 ? -self : self end @@ -170,7 +170,7 @@ struct Number # 0.sign # => 0 # -42.sign # => -1 # ``` - def sign + def sign : Int32 self < 0 ? -1 : (self == 0 ? 0 : 1) end