# Multi precision Rational numbers.

``````assert((0.2 + 0.1) == 0.30000000000000004)
assert(("1/5".to_r + "1/10".to_r) == "3/10".to_r)``````

### Introduced properties

##### fun abs: Ratio

gmp :: Ratio :: abs

The absolute value of `self`.
##### init defaultinit(val: NativeMPQ)

gmp :: Ratio :: defaultinit

### Redefined properties

##### redef fun *(r: OTHER): OTHER

gmp \$ Ratio :: *

`assert("3/2".to_r * 2.to_r == 3.to_r)`
##### redef fun +(r: OTHER): OTHER

gmp \$ Ratio :: +

`assert("3/2".to_r + "5/2".to_r == 4.to_r)`
##### redef fun -(r: OTHER): OTHER

gmp \$ Ratio :: -

`assert("5/2".to_r - "3/2".to_r == 1.to_r)`
##### redef fun /(r: OTHER): OTHER

gmp \$ Ratio :: /

`assert(3.to_r / 2.to_r == "3/2".to_r)`
##### redef fun <(r: OTHER): Bool

gmp \$ Ratio :: <

Is `self` lesser than `other`?
##### redef fun <=(r: OTHER): Bool

gmp \$ Ratio :: <=

not `other` < `self`
##### redef fun <=>(r: OTHER): Int

gmp \$ Ratio :: <=>

-1 if <, +1 if > and 0 otherwise
##### redef fun ==(r: nullable Object): Bool

gmp \$ Ratio :: ==

Have `self` and `other` the same value?
##### redef fun >(r: OTHER): Bool

gmp \$ Ratio :: >

`other` < `self`
##### redef fun >=(r: OTHER): Bool

gmp \$ Ratio :: >=

not `self` < `other`
##### redef type OTHER: Ratio

gmp \$ Ratio :: OTHER

What `self` can be compared to?
##### redef type SELF: Ratio

gmp \$ Ratio :: SELF

Type of this instance, automatically specialized in every class
##### redef fun finalize_once

gmp \$ Ratio :: finalize_once

Real finalization method of `FinalizableOnce`, will be called only once
##### redef fun hash: Int

gmp \$ Ratio :: hash

The hash code of the object.
##### redef fun to_bi: BigInt

gmp \$ Ratio :: to_bi

`assert("7/2".to_r.to_bi == 3.to_bi)`
##### redef fun to_f: Float

gmp \$ Ratio :: to_f

`assert(3.to_r.to_f == 3.0)`
##### redef fun to_i: Int

gmp \$ Ratio :: to_i

`assert("7/2".to_r.to_i == 3)`
##### redef fun to_r: Ratio

gmp \$ Ratio :: to_r

The Ratio equivalent of `self`
##### redef fun to_s: String

gmp \$ Ratio :: to_s

`assert(3.to_r.to_s == "3")`
##### redef fun unary -: OTHER

gmp \$ Ratio :: unary -

`assert( -("1/2".to_r) == ("-1/2").to_r)`
##### redef fun value_of(n: Numeric): OTHER

gmp \$ Ratio :: value_of

`assert((3.to_r).value_of(2) == 2.to_r)`
##### redef fun zero: OTHER

gmp \$ Ratio :: zero

`assert((3.to_r).zero == 0.to_r)`

## All properties

##### fun !=(other: nullable Object): Bool

core :: Object :: !=

Have `self` and `other` different values?
##### abstract fun *(i: OTHER): OTHER

core :: Numeric :: *

Multiplication of `self` with `i`
##### abstract fun +(i: OTHER): OTHER

core :: Numeric :: +

Addition of `self` with `i`
##### abstract fun -(i: OTHER): OTHER

core :: Numeric :: -

Substraction of `i` from `self`
##### abstract fun /(i: OTHER): OTHER

core :: Numeric :: /

Division of `self` with `i`
##### abstract fun <(other: OTHER): Bool

core :: Comparable :: <

Is `self` lesser than `other`?
##### fun <=(other: OTHER): Bool

core :: Comparable :: <=

not `other` < `self`
##### fun <=>(other: OTHER): Int

core :: Comparable :: <=>

-1 if <, +1 if > and 0 otherwise
##### fun ==(other: nullable Object): Bool

core :: Object :: ==

Have `self` and `other` the same value?
##### fun >(other: OTHER): Bool

core :: Comparable :: >

`other` < `self`
##### fun >=(other: OTHER): Bool

core :: Comparable :: >=

not `self` < `other`
##### type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
##### type OTHER: Comparable

core :: Comparable :: OTHER

What `self` can be compared to?
##### type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
##### fun abs: Ratio

gmp :: Ratio :: abs

The absolute value of `self`.
##### abstract fun add(other: Numeric): Numeric

Universal `+` with any `Numeric`
##### fun clamp(min: OTHER, max: OTHER): OTHER

core :: Comparable :: clamp

Constraint `self` within `[min..max]`
##### protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by `get_class` to create the specific class.
##### fun class_name: String

core :: Object :: class_name

The class name of the object.
##### init defaultinit

core :: Finalizable :: defaultinit

##### init defaultinit

core :: FinalizableOnce :: defaultinit

##### init defaultinit

core :: Numeric :: defaultinit

##### init defaultinit(val: NativeMPQ)

gmp :: Ratio :: defaultinit

##### init defaultinit

core :: Comparable :: defaultinit

##### init defaultinit

core :: Object :: defaultinit

##### abstract fun div(other: Numeric): Numeric

core :: Numeric :: div

Universal `/` with any `Numeric`
##### fun finalize

core :: Finalizable :: finalize

Liberate any resources held by `self` before the memory holding `self` is freed
##### protected fun finalize_once

core :: FinalizableOnce :: finalize_once

Real finalization method of `FinalizableOnce`, will be called only once
##### fun finalized: Bool

core :: FinalizableOnce :: finalized

Has `self` been finalized? (either by the GC or an explicit call to `finalize`)
##### protected fun finalized=(finalized: Bool)

core :: FinalizableOnce :: finalized=

Has `self` been finalized? (either by the GC or an explicit call to `finalize`)
##### fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of `self`.
##### fun hash: Int

core :: Object :: hash

The hash code of the object.
##### init init

core :: Object :: init

##### fun inspect: String

core :: Object :: inspect

Developer readable representation of `self`.

Return "CLASSNAME:#OBJECTID".
##### fun is_between(c: OTHER, d: OTHER): Bool

core :: Comparable :: is_between

c <= self <= d
##### intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if `self` and `other` are the same instance (i.e. same identity).
##### fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is `self` the same as `other` in a serialization context?
##### intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if `self` and `other` have the same dynamic type.
##### fun is_zero: Bool

core :: Numeric :: is_zero

Is this the value of zero in its domain?
##### fun max(other: OTHER): OTHER

core :: Comparable :: max

The maximum between `self` and `other` (prefers `self` if equals).
##### fun min(c: OTHER): OTHER

core :: Comparable :: min

The minimum between `self` and `c` (prefer `self` if equals)
##### abstract fun mul(other: Numeric): Numeric

core :: Numeric :: mul

Universal `*` with any `Numeric`
##### intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
##### fun output

core :: Object :: output

Display self on stdout (debug only).
##### intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
##### fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
##### abstract fun sub(other: Numeric): Numeric

core :: Numeric :: sub

Universal `-` with any `Numeric`
##### intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the `Sys` class.
##### abstract fun to_b: Byte

core :: Numeric :: to_b

The byte equivalent of `self`
##### fun to_bi: BigInt

core :: Numeric :: to_bi

The BigInt equivalent of `self`
##### abstract fun to_f: Float

core :: Numeric :: to_f

The float equivalent of `self`
##### abstract fun to_i: Int

core :: Numeric :: to_i

The integer part of `self`.
##### fun to_i16: Int16

core :: Numeric :: to_i16

The Int16 equivalent of `self`
##### fun to_i32: Int32

core :: Numeric :: to_i32

The Int32 equivalent of `self`
##### fun to_i8: Int8

core :: Numeric :: to_i8

The Int8 equivalent of `self`
##### abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

##### fun to_r: Ratio

core :: Numeric :: to_r

The Ratio equivalent of `self`
##### fun to_s: String

core :: Object :: to_s

User readable representation of `self`.
##### fun to_u16: UInt16

core :: Numeric :: to_u16

The UInt16 equivalent of `self`
##### fun to_u32: UInt32

core :: Numeric :: to_u32

The UInt32 equivalent of `self`
##### abstract fun unary -: OTHER

core :: Numeric :: unary -

Inverse of `self`
##### abstract fun value_of(val: Numeric): OTHER

core :: Numeric :: value_of

The value of `val` in the domain of `self`
##### abstract fun zero: OTHER

core :: Numeric :: zero

The value of zero in the domain of `self`

### Ancestors

##### interface Comparable

core :: Comparable

The ancestor of class where objects are in a total order.
##### class Finalizable

core :: Finalizable

An object needing finalization
##### interface Object

core :: Object

The root of the class hierarchy.

### Parents

##### class FinalizableOnce

core :: FinalizableOnce

An object to be finalized only once
##### interface Numeric

core :: Numeric

A numeric value supporting mathematical operations

## Class definitions

##### gmp \$ Ratio
```# Multi precision Rational numbers.
#
#     assert((0.2 + 0.1) == 0.30000000000000004)
#     assert(("1/5".to_r + "1/10".to_r) == "3/10".to_r)
class Ratio
super Numeric
super FinalizableOnce

redef type OTHER: Ratio

private var val: NativeMPQ

redef fun hash do return self.to_i

redef fun <=>(r) do
var res = val.cmp(r.val)
if (res) < 0 then
return -1
else if (res) > 0 then
return 1
else
return 0
end
end

redef fun ==(r) do return r isa Ratio and (self <=> r) == 0
redef fun <=(r) do return (self <=> r) <= 0
redef fun <(r) do return (self <=> r) < 0
redef fun >=(r) do return (self <=> r) >= 0
redef fun >(r) do return (self <=> r) > 0

#     assert("3/2".to_r + "5/2".to_r == 4.to_r)
redef fun +(r) do
var res = new NativeMPQ
return new Ratio(res)
end

#     assert( -("1/2".to_r) == ("-1/2").to_r)
redef fun - do
var res = new NativeMPQ
val.neg res
return new Ratio(res)
end

#     assert("5/2".to_r - "3/2".to_r == 1.to_r)
redef fun -(r) do
var res = new NativeMPQ
val.sub(res, r.val)
return new Ratio(res)
end

#     assert("3/2".to_r * 2.to_r == 3.to_r)
redef fun *(r) do
var res = new NativeMPQ
val.mul(res, r.val)
return new Ratio(res)
end

#     assert(3.to_r / 2.to_r == "3/2".to_r)
redef fun /(r) do
var res = new NativeMPQ
val.div(res, r.val)
return new Ratio(res)
end

# The absolute value of `self`.
#
#     assert((-3.to_r).abs == 3.to_r)
#     assert(3.to_r.abs == 3.to_r)
fun abs: Ratio do
var res = new NativeMPQ
val.abs res
return new Ratio(res)
end

#     assert((3.to_r).zero == 0.to_r)
redef fun zero do return new Ratio(new NativeMPQ)

#     assert((3.to_r).value_of(2) == 2.to_r)
redef fun value_of(n) do return n.to_r

#     assert("7/2".to_r.to_i == 3)
redef fun to_i do
var res = new NativeMPZ
val.numref.tdiv_q(res, val.denref)
return res.get_si
end

#     assert(3.to_r.to_f == 3.0)
redef fun to_f do return val.get_d

#     assert(3.to_r.to_s == "3")
redef fun to_s do
var cstr = val.get_str(10i32)
var str = cstr.to_s
cstr.free
return str
end

#     assert("7/2".to_r.to_bi == 3.to_bi)
redef fun to_bi do
var res = new NativeMPZ
val.numref.tdiv_q(res, val.denref)
return new BigInt(res)
end

redef fun to_r do return self

redef fun finalize_once do val.finalize
end
```
lib/gmp/gmp.nit:289,1--399,3