Skip to content

Base Conversion

Fermat can print your number in many different bases without needing to worry about how that might change the calculations. All calculations will continue to work correctly, no matter the base your number is in. It only effects the printing.

Available Bases

The following bases are supported in Fermat:

  • Base-2: NumberBase::Two
  • Base-3: NumberBase::Three
  • Base-4: NumberBase::Four
  • Base-5: NumberBase::Five
  • Base-6: NumberBase::Six
  • Base-7: NumberBase::Seven
  • Base-8: NumberBase::Eight
  • Base-9: NumberBase::Nine
  • Base-10: NumberBase::Ten
  • Base-12: NumberBase::Twelve
  • Base-16: NumberBase::Sixteen

Changing The Base On A Number

Any number can have its base changed at any time. To do this, you simply use the setBase(NumberBase $base) method. Once this is set, all calls to getValue() without arguments will print in that base.

Alternatively, you can provide the desired base as part of the getValue() call to have it export in that base just once.

 1<?php
 2
 3use Samsara\Fermat\Core\Core\Values\ImmutableDecimal;
 4use Samsara\Fermat\Core\Core\Enums\NumberBase;
 5
 6$five = new ImmutableDecimal(5);
 7
 8// NOTE: setBase() is mutable for all values
 9$five->setBase(NumberBase::Four);
10
11echo $five->getValue();                 // Prints: 11
12echo $five->getValue(NumberBase::Five); // Prints: 10
13echo $five->getValue(NumberBase::Two);  // Prints: 101
14echo $five->getValue(NumberBase::Three);// Prints: 12

Setting The Base During Instantiation

You can also set the base of an object while it is being instantiated.

 1<?php
 2
 3use Samsara\Fermat\Core\Core\Values\ImmutableDecimal;
 4use Samsara\Fermat\Core\Core\Enums\NumberBase;
 5use Samsara\Fermat\Core\Core\Numbers;
 6
 7$five = new ImmutableDecimal(5, null, NumberBase::Five);
 8
 9echo $five->getValue(); // Prints: 10
10
11$six = Numbers::make(Numbers::IMMUTABLE, 6, null, NumberBase::Three);
12
13echo $six->getValue();  // Prints: 20

Instantiating With Non-Base-10 Input

If you want Fermat to assume the value you pass during instantiation is already in the target base, you can set the $baseTenInput argument to false.

1<?php
2
3use Samsara\Fermat\Core\Core\Values\ImmutableDecimal;
4use Samsara\Fermat\Core\Core\Enums\NumberBase;
5
6$five = new ImmutableDecimal(10, null, NumberBase::Five, false);
7
8echo $five->getValue();                // Prints: 10
9echo $five->getValue(NumberBase::Ten); // Prints: 5

This is especially useful if you want to, for instance, convert a hex number into a Fermat value.

 1<?php
 2
 3use Samsara\Fermat\Core\Core\Values\ImmutableDecimal;
 4use Samsara\Fermat\Core\Core\Enums\NumberBase;
 5
 6$color = "FF9933";
 7$moreGreen = "003300";
 8
 9$baseColor = new ImmutableDecimal(
10    value: $color, 
11    base: NumberBase::Sixteen, 
12    baseTenInput: false
13);
14$colorChange = new ImmutableDecimal(
15    value: $moreGreen,
16    base: NumberBase::Sixteen,
17    baseTenInput: false
18);
19
20$newColor = $baseColor->add($colorChange);
21
22echo $newColor->getValue(); // Prints: FFCC33