...
 
Commits (1)
  • Ryan Frazier's avatar
    Add decimal support · 7505b56c
    Ryan Frazier authored
    PROBLEM!
    The decimal input introduces float rounding errors. Need to fix this.
    7505b56c
......@@ -13,6 +13,7 @@ import Html.Events exposing (onClick)
type alias Model =
{ stack : List Float
, currentNum : Float
, decimal : Float
}
......@@ -20,6 +21,7 @@ initialModel : Model
initialModel =
{ stack = []
, currentNum = 0
, decimal = 1
}
......@@ -69,21 +71,24 @@ operatorFunction op =
type Msg
= InputOperator Operator
| InputNumber Float
| InputDecimal
| Clear
| ClearAll
| Back
| Enter
| SetDecimal
update : Msg -> Model -> Model
update msg model =
case msg of
SetDecimal ->
{ model | decimal = 0.1 }
ClearAll ->
{ model | currentNum = 0, stack = [] }
{ model | currentNum = 0, stack = [], decimal = 1 }
Clear ->
{ model | currentNum = 0 }
{ model | currentNum = 0, decimal = 1 }
Back ->
{ model | currentNum = toFloat <| floor <| model.currentNum / 10 }
......@@ -92,6 +97,7 @@ update msg model =
{ model
| stack = model.currentNum :: model.stack
, currentNum = 0
, decimal = 1
}
InputOperator operator ->
......@@ -113,10 +119,19 @@ update msg model =
}
InputNumber num ->
{ model | currentNum = (model.currentNum * 10) + num }
if model.decimal < 1 then
{ model
| currentNum =
(num * model.decimal) + model.currentNum
, decimal =
model.decimal * 0.1
}
InputDecimal ->
model
else
{ model
| currentNum =
(model.currentNum * 10) + num
}
......@@ -192,7 +207,7 @@ section =
, cell (onClick <| InputNumber 3) Single White "3"
, cell (onClick <| InputOperator Add) Single Yellow "+"
, cell (onClick <| InputNumber 0) Single White "0"
, cell (onClick InputDecimal) Single White "."
, cell (onClick SetDecimal) Single White "."
, cell (onClick <| Enter) Double Yellow "Enter"
]
......