roundTo:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

roundTo:

J Pfersich
st> -8 roundTo: 3 !
-6       ===============> incorrect, should be -9
st> 8 roundTo: 3 !
9        ===============>  correct
The ANSI definition is:

roundTo: factor
Answer the number nearest the receiver that is a multiple of factor.
As far as I can see these statements should be true:

-8 roundTo: 3 yields -9
8 roundTo: -3 yields 9
25 roundTo: -7 yields 28
-25 roundTo: 7 yields -28
-27 roundTo: 4 yields -28
27 roundTo: -4 yields 28
It doesn't now. Fixing roundTo: may break the uses in Point.



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: roundTo:

Paolo Bonzini
J Pfersich wrote:
> st> -8 roundTo: 3 !
> -6       ===============> incorrect, should be -9
> st> 8 roundTo: 3 !
> 9        ===============>  correct

Right.  And there's another bug a couple lines above:

st> ((30 factorial + (1/4)) rounded - 30 factorial) printNl!
9581293239009280

Both fixed with the attached patch.  Thank you.

Paolo

--- orig/kernel/Number.st
+++ mod/kernel/Number.st
@@ -400,14 +400,14 @@ truncateTo: aNumber
 rounded
     "Returns the integer nearest the receiver"
     ^self negative
- ifTrue: [ (self - 0.5) ceiling ]
- ifFalse: [ (self + 0.5) floor ]
+ ifTrue: [ (self - (self unity / 2)) ceiling ]
+ ifFalse: [ (self + (self unity / 2)) floor ]
 !
 
 roundTo: aNumber
     "Answer the receiver, truncated to the nearest multiple
      of aNumber"
-    ^(self + (aNumber / 2) / aNumber) integerPart * aNumber
+    ^(self / aNumber) rounded * aNumber
 ! !
 
 

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk