Skip to content

Commit

Permalink
Merge pull request #370 from Don-Ward/FxPt-Correction
Browse files Browse the repository at this point in the history
Corrections to the fixed point arithmetic class.
  • Loading branch information
Jafaral authored Mar 5, 2024
2 parents e6cfe20 + be1a62d commit 3a71574
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions tests/unicon/FxPtTests.icn
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ procedure test_division()
T.verify {"(-5/6)*(6/-5) = 1", FxPt(-5).div(six).mul(six.div(-5)).round(50).eq(1) }
T.verify {"(5/-6)*(6/-5) = 1", five.div(-6).mul(six.div(-5)).round(50).eq(1) }

T.verify {"1.div(6,10)", FxPt(1).div(six,10).toString() == "0.1666666667" }
T.verify {"4.div(6,15)", four.div(six,15).toString() == "0.666666666666667" }
T.verify {"9.div(6,3)", FxPt(9).div(six,3).toString() == "1.500" }

# This test takes 99.9% of the run time; disable it if you're repeatedly running tests
# by uncommenting the next line.
# return
Expand Down
2 changes: 1 addition & 1 deletion tests/unicon/stand/FxPtTest.std
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--------------------------------------------------
Fixed Point Tests Start
Fixed Point Tests Passed = 454 Failed = 0
Fixed Point Tests Passed = 457 Failed = 0
--------------------------------------------------
2 changes: 1 addition & 1 deletion tests/unicon/stand/FxPtTest_OVLD.std
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--------------------------------------------------
Fixed Point Tests Start
Fixed Point Tests Passed = 586 Failed = 0
Fixed Point Tests Passed = 589 Failed = 0
--------------------------------------------------
Empty file.
8 changes: 5 additions & 3 deletions uni/lib/FxPt.icn
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ class FxPt: _FxPt()
# Divide another fixed-point value or number from this FxPt.
# <[returns result of the division as a FxPt]>
#</p>
method div(fp # fixed-point of number
method div(fp, # fixed-point of number
places # places
)
return FxPt(self$_FxPt.div(_toFxPt(fp)))
return FxPt(self$_FxPt.div(_toFxPt(fp), places))
end

#<p>
Expand Down Expand Up @@ -494,7 +495,8 @@ $endif # FXPT_DEBUG
end

method div(fp, places) # divide this by fp
return self.mul(fp._reciprocal(places))
/places := 200 # We have to stop somewhere
return self.mul(fp._reciprocal(places+1)).round(places)
end

method round(p1) # round away from zero to p1 decimal places
Expand Down

0 comments on commit 3a71574

Please sign in to comment.