NAME

Sidef::Types::Number::Fraction - Symbolic support for fractions

DESCRIPTION

This class implements symbolic support for rational values (fractions) in Sidef. It inherits from Sidef::Types::Number::Number.

SYNOPSIS

var f = Fraction(3, 4)
say f.class         # Fraction

var a = Fraction(1, 2)
var b = Fraction(1, 3)

say (a + b)         # Fraction(5, 6)
say (a - b)         # Fraction(1, 6)
say (a * b)         # Fraction(1, 6)
say (a / b)         # Fraction(3, 2)

say f.numerator     # 3
say f.denominator   # 4
say f.as_float      # 0.75

INHERITANCE

Sidef::Types::Number::Fraction
Sidef::Types::Number::Number
Sidef::Object::Object

METHODS

new

Fraction(num, den)

Creates a new Fraction object with the given numerator num and denominator den.

numerator

f.numerator
f.num

Returns the numerator of the fraction as an integer.

denominator

f.denominator
f.den

Returns the denominator of the fraction as an integer. The denominator is always positive.

parts

f.parts

Returns a list containing the numerator and the denominator.

int

f.int
f.to_i

Returns the integer part of the fraction (truncates towards zero).

float

f.float
f.to_f

Returns the fraction converted to a floating-point number (Sidef Float).

rat

f.rat

Returns the object itself, as it is already a rational number.

inv

f.inv

Returns the multiplicative inverse (reciprocal) of the fraction ($1/f$).

abs

f.abs

Returns the absolute value of the fraction.

neg

f.neg
-f

Returns the negation of the fraction.

inc

f.inc

Returns the fraction incremented by 1.

dec

f.dec

Returns the fraction decremented by 1.

sign

f.sign

Returns -1 if the fraction is negative, 0 if it is zero, and 1 if it is positive.

ceil

f.ceil

Returns the smallest integer greater than or equal to the fraction.

floor

f.floor

Returns the largest integer less than or equal to the fraction.

truncate

f.truncate

Truncates the fraction to an integer (towards zero).

round

f.round

Rounds the fraction to the nearest integer.

is_pos

f.is_pos

Returns true if the fraction is strictly positive.

is_neg

f.is_neg

Returns true if the fraction is strictly negative.

is_zero

f.is_zero

Returns true if the fraction is equal to zero.

is_one

f.is_one

Returns true if the fraction is equal to one.

is_int

f.is_int

Returns true if the fraction represents an integer (denominator is 1).

is_proper

f.is_proper

Returns true if the absolute value of the fraction is less than 1 (numerator < denominator).

is_improper

f.is_improper

Returns true if the absolute value of the fraction is greater than or equal to 1.

as_mixed

f.as_mixed

Returns a list of three elements representing the mixed fraction: integer part, numerator of the remainder, and the denominator.

var f = Fraction(5, 3)
say f.as_mixed   # [1, 2, 3]  (representing 1 2/3)

to_s

f.to_s

Returns the string representation of the fraction (e.g., "Fraction(3, 4)").

dump

f.dump

Returns a string representation of the object for debugging purposes.

OPERATORS

The following operators are overloaded for Sidef::Types::Number::Fraction:

  • + (Addition)

  • - (Subtraction)

  • * (Multiplication)

  • / (Division)

  • % (Modulo)

  • ** (Exponentiation)

  • <=> (Comparison)

Additionally, standard boolean comparison operators (==, !=, <, <=, >, >=) work as expected.

AUTHOR

Daniel "Trizen" Șuteu, <trizen@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2013-2025 by Daniel "Trizen" Șuteu

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.26.1 or, at your option, any later version of Perl 5 you may have available.