Skip to content

Commit

Permalink
std::math::big: add octal format support to the Int for string conver…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
mertcandav committed Mar 11, 2024
1 parent 0b82a80 commit b986370
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions std/math/big/conv.jule
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ fn format_common(&b: bits, base: byte): []byte {
fn format_decimal(&b: bits): []byte {
ret format_common(b, 10)
}

fn format_octal(&b: bits): []byte {
ret format_common(b, 8)
}
1 change: 1 addition & 0 deletions std/math/big/int.jule
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ impl Int {
//
// Valid fmt values are;
// - 2 for binary.
// - 8 for binary.
// - 10 for decimal.
pub fn format(self, fmt: int)!: str {
let mut s = self.nat.format(fmt) else { error(error) }
Expand Down
23 changes: 16 additions & 7 deletions std/math/big/int_test.jule
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,21 @@ fn test_int_format(mut t: &T) {
t.assert((Int.parse("-00111", 2)!.format(2)!) == "-111", "4) 00111 != -111")
t.assert((Int.parse("1010", 2)!.format(2)!) == "1010", "5) 1010 != 1010")

// Decimal
t.assert((Int.parse("1010", 2)!.format(10)!) == "10", "6) 1010 != 10")
t.assert((Int.parse("101", 2)!.format(10)!) == "5", "7) 101 != 5")
t.assert((Int.parse("-1010", 2)!.format(10)!) == "-10", "8) -1010 != -10")
t.assert((Int.parse("-101", 2)!.format(10)!) == "-5", "9) -101 != -5")
t.assert((Int.parse("1111010101", 2)!.format(10)!) == "981", "10) 1010 != 981")
t.assert((Int.parse("-10001101101110101011110110000110111111101101", 2)!.format(10)!) == "-9739573948397", "11) -10001101101110101011110110000110111111101101 != -9739573948397")
// Octal
t.assert((Int.parse("1010", 2)!.format(8)!) == "12", "6) 1010 != 12")
t.assert((Int.parse("101", 2)!.format(8)!) == "5", "7) 101 != 5")
t.assert((Int.parse("-1010", 2)!.format(8)!) == "-12", "8) -1010 != -12")
t.assert((Int.parse("-101", 2)!.format(8)!) == "-5", "9) -101 != -5")
t.assert((Int.parse("1111010101", 2)!.format(8)!) == "1725", "10) 1111010101 != 1725")
t.assert((Int.parse("-10001101101110101011110110000110111111101101", 2)!.format(8)!) == "-215565366067755", "11) -10001101101110101011110110000110111111101101 != -215565366067755")
t.assert((Int.parse("100111001110111001111001111111010101001001011000000000110101110110111110110000101101001", 2)!.format(8)!) == "47167171772511300065667660551", "12) 100111001110111001111001111111010101001001011000000000110101110110111110110000101101001 != 47167171772511300065667660551")

// Decimal
t.assert((Int.parse("1010", 2)!.format(10)!) == "10", "13) 1010 != 10")
t.assert((Int.parse("101", 2)!.format(10)!) == "5", "14) 101 != 5")
t.assert((Int.parse("-1010", 2)!.format(10)!) == "-10", "15) -1010 != -10")
t.assert((Int.parse("-101", 2)!.format(10)!) == "-5", "16) -101 != -5")
t.assert((Int.parse("1111010101", 2)!.format(10)!) == "981", "17) 1111010101 != 981")
t.assert((Int.parse("-10001101101110101011110110000110111111101101", 2)!.format(10)!) == "-9739573948397", "18) -10001101101110101011110110000110111111101101 != -9739573948397")
t.assert((Int.parse("100111001110111001111001111111010101001001011000000000110101110110111110110000101101001", 2)!.format(10)!) == "94859300696293528418869609", "19) 100111001110111001111001111111010101001001011000000000110101110110111110110000101101001 != 94859300696293528418869609")
}
3 changes: 3 additions & 0 deletions std/math/big/nat.jule
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,14 @@ impl Nat {
//
// Valid fmt values are;
// - 2 for binary.
// - 8 for binary.
/// - 10 for decimal.
pub fn format(self, fmt: int)!: str {
match fmt {
| 2:
ret str(format_binary(self.bits))
| 8:
ret str(format_octal(self.bits))
| 10:
ret str(format_decimal(self.bits))
|:
Expand Down

0 comments on commit b986370

Please sign in to comment.