PHP code example of scruoge / lib-fractions

1. Go to this page and download the library: Download scruoge/lib-fractions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

scruoge / lib-fractions example snippets




use Scruoge\Fractions\RationalExp;

// Create rational numbers
$a = RationalExp::fromNumber(2.3);        // 2.3 as exact fraction
$b = RationalExp::create(120, -2);        // 120 * 10^(-2) = 1.2
$c = RationalExp::zero();                 // 0
$d = RationalExp::one();                  // 1

// Perform arithmetic operations
$sum = $a->add($b);                       // 2.3 + 1.2 = 3.5
$difference = $a->sub($b);                // 2.3 - 1.2 = 1.1
$product = $a->mul($b);                   // 2.3 * 1.2 = 2.76
$quotient = $a->div($b);                  // 2.3 / 1.2 = 1.916...

// Convert back to float
echo $sum->toFloat();                     // 3.5

// Check equality
$isEqual = $a->equals($b);                // false

// Creates 123 * 10^(-2) / 4 = 1.23/4 = 0.3075
$rational = RationalExp::create(123, -2, 4);

// Automatic precision detection
$auto = RationalExp::fromNumber(1234.2345);

// Fixed precision with rounding
$rounded = RationalExp::fromNumber(1234.2345, 3);             // 3 decimal places
$roundDown = RationalExp::fromNumber(1234.2345, 3, PHP_ROUND_HALF_DOWN);

$zero = RationalExp::zero();    // 0
$one = RationalExp::one();      // 1

$a = RationalExp::fromNumber(2.5);
$b = RationalExp::fromNumber(1.5);

$addition = $a->add($b);        // 4.0
$subtraction = $a->sub($b);     // 1.0
$multiplication = $a->mul($b);  // 3.75
$division = $a->div($b);        // 1.666...

$a = RationalExp::fromNumber(0.1);
$b = RationalExp::fromNumber(0.1);
$isEqual = $a->equals($b);      // true

$rational = RationalExp::create(1, 0, 3);
$float = $rational->toFloat();  // 0.3333...

$rational = RationalExp::create(1, 2, 3);
$json = json_encode($rational);
// {"significand":1,"denominator":3,"exponent":2}

// Create 3/8
$fraction = RationalExp::create(3, 0, 8);

// Create 1/3 (which cannot be represented exactly as a decimal)
$oneThird = RationalExp::create(1, 0, 3);

$value = 1234.2345;

// Automatic precision (detects significant digits)
$auto = RationalExp::fromNumber($value);

// Fixed precision
$fixed = RationalExp::fromNumber($value, 2);  // 1234.23

// Different rounding modes
$halfUp = RationalExp::fromNumber($value, 2, PHP_ROUND_HALF_UP);    // 1234.23
$halfDown = RationalExp::fromNumber($value, 2, PHP_ROUND_HALF_DOWN); // 1234.23
$up = RationalExp::fromNumber($value, 2, PHP_ROUND_UP);            // 1234.24

$price = RationalExp::fromNumber(19.99);
$taxRate = RationalExp::fromNumber(0.08);
$quantity = RationalExp::fromNumber(3);

$subtotal = $price->mul($quantity);           // 59.97
$tax = $subtotal->mul($taxRate);              // 4.7976
$total = $subtotal->add($tax);                // 64.7676

// Round to cents for final amount
$finalTotal = RationalExp::fromNumber($total->toFloat(), 2);  // 64.77
bash
composer cs-fix
# or
./vendor/bin/php-cs-fixer fix

lib-fractions/
├── src/
│   └── RationalExp.php      # Main rational number class
├── test/
│   └── RationalExpTest.php  # Unit tests
├── composer.json            # Project configuration
├── phpunit.xml             # PHPUnit configuration
└── .php-cs-fixer.dist.php  # Code style configuration