Skip to content

Commit

Permalink
Support negative inputs in fraction.from_float_string (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 authored Feb 6, 2024
1 parent 5f5a416 commit b48e105
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .spec/math/fraction_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe("Fraction", function()
{ base = 2, str = "0.(10)", val = frac(2, 3) },
{ base = nil, str = "3.(3)", val = frac(10, 3) }, -- test default base
{ base = 10, str = "1.2(3)", val = frac(12, 10) + frac(1, 30) },
{ base = 10, str = "1", val = frac(1, 1) },
{ base = 10, str = "-2", val = frac(-2, 1) },
{ base = 10, str = "-1.25", val = frac(-5, 4) },
{ base = 10, str = "-1.3(51)", val = frac(-223, 165) },
{ base = 16, str = "1.(45d17)", val = frac(42, 33) },
}

Expand Down
15 changes: 13 additions & 2 deletions src/math/fraction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ local function read_base_param(base)
return base
end

function fraction.from_float_string(
str, -- <digit>{<digit>}.{digit}[(<digit>{digit})], ex.: `-1.2(3)`
local function parse_positive_double_string(
str, -- EBNF: digit { digit } "." { digit } [ "(" digit { digit } ")" ], ex.: `1.2(3)`
base -- integer from 2 to 36, defaults to 10 (decimal)
)
base = read_base_param(base)
Expand All @@ -81,6 +81,7 @@ function fraction.from_float_string(

local integer, fractional = str:match("^([0-9a-zA-Z][0-9a-zA-Z]-)%.([0-9a-zA-Z%(%)]+)")
if not fractional then
assert(str:match("[0-9a-zA-Z]+"))
return new(read_number(str), 1)
end

Expand All @@ -96,6 +97,16 @@ function fraction.from_float_string(
return read_number(integer) + after_dot / base ^ #pre_period
end

function fraction.from_float_string(
str, -- EBNF: [ "-" ] digit { digit } "." { digit } [ "(" digit { digit } ")" ], ex.: `-1.2(3)`
base -- integer from 2 to 36, defaults to 10 (decimal)
)
if str:sub(1, 1) == "-" then
return -parse_positive_double_string(str:sub(2), base)
end
return parse_positive_double_string(str, base)
end

-- Conversions

function metatable:__tostring()
Expand Down

0 comments on commit b48e105

Please sign in to comment.