I couldn't find such a function. I tryed in the method finder but find nothing: MethodFinder methodFor: #(#(1.2288 3) 1.228).
Do somebody know if such a method exist ?
Meanwhile I did that:
Float>>trimAfter: aNumberOfDecimal "roundAfter:" | number | number := 10 raisedTo: aNumberOfDecimal. ^(self * 10^aNumber) truncated "rounded" / 10^aNumber asFloat
and still MethodFinder methodFor: #(#(1.2288 3) 1.228) returns '(no single method does that function) ' whereas 1.2288 roundedAfter: 3 returns 1.229
Does anybody know what was wrong in my method finder use ?
Thanks
Cédrick
Hello Cédrick,
c> I couldn't find such a function. I tryed in the method finder but find c> nothing: MethodFinder methodFor: #(#(1.2288 3) 1.228).
afaik method finder doesn't find *every* method but tries a a fixed set of messages. Methods that would do the job but are not in the list will not be found.
MethodFinder>>initialize and friends suggest this.
Cheers,
Herbert
afaik method finder doesn't find *every* method but tries a a fixed set of messages. Methods that would do the job but are not in the list will not be found.
MethodFinder>>initialize and friends suggest this.
stupid me :)
but.. I think I'll read these methods another day...
Thanks Herbert
So anybody has an idea of methods that truncate/round Floats ?
Hello Cédrick,
c> So anybody has an idea of methods that truncate/round Floats ? sorry for ignoring your main question.
Float has a truncate and round off protocal with metods that truncate and round to integer.
So it looks you have to do something like
(aFloat * (10^desiredDecimals) + 0.5) truncated / (10^desiredDecimals)
or without the + 0.5 for truncation.
Cheers,
Herbert
Float has a truncate and round off protocal with metods that truncate and round to integer.
So it looks you have to do something like
(aFloat * (10^desiredDecimals) + 0.5) truncated / (10^desiredDecimals)
or without the + 0.5 for truncation.
close to what I proposed earlier ;-) Just need a conversion to Float as it returns a fraction.
so no existing method... Isn't it something common ?
Are #truncateTo: and #roundTo: what you want?
1.2288 roundTo: 1 "1" 1.2288 roundTo: 0.1 "1.2" 1.2288 roundTo: 0.01 "1.23" 1.2288 roundTo: 0.001 "1.229" 1.2288 roundTo: 0.0001 "1.2288" 1.2288 roundTo: 0.05 "1.25" 1.2288 roundTo: 0.002 "1.228"
1.2288 truncateTo: 1 "1" 1.2288 truncateTo: 0.1 "1.2" 1.2288 truncateTo: 0.01 "1.22" 1.2288 truncateTo: 0.001 "1.228" 1.2288 truncateTo: 0.0001 "1.2288" 1.2288 truncateTo: 0.05 "1.2" 1.2288 truncateTo: 0.002 "1.228"
Z.
cdrick wrote:
So anybody has an idea of methods that truncate/round Floats ?
1.2288 roundTo: 1 "1" 1.2288 roundTo: 0.1 "1.2" 1.2288 roundTo: 0.01 "1.23" 1.2288 roundTo: 0.001 "1.229" 1.2288 roundTo: 0.0001 "1.2288" 1.2288 roundTo: 0.05 "1.25" 1.2288 roundTo: 0.002 "1.228"
Stupid me again ;)
I only tried #roundTo: with Integer.
But #printShowingDecimalPlaces: is alright ;)
Thanks...
Cédrick
cdrick a écrit :
1.2288 roundTo: 1 "1" 1.2288 roundTo: 0.1 "1.2" 1.2288 roundTo: 0.01 "1.23" 1.2288 roundTo: 0.001 "1.229" 1.2288 roundTo: 0.0001 "1.2288" 1.2288 roundTo: 0.05 "1.25" 1.2288 roundTo: 0.002 "1.228"
Stupid me again ;)
I only tried #roundTo: with Integer.
But #printShowingDecimalPlaces: is alright ;)
Thanks...
Cédrick
You might want to check for http://bugs.squeak.org/view.php?id=5640
Installer mantis ensureFix: 5640.
cdrick a écrit :
I couldn't find such a function. I tryed in the method finder but find nothing: MethodFinder methodFor: #(#(1.2288 3) 1.228).
Do somebody know if such a method exist ?
Meanwhile I did that:
Float>>trimAfter: aNumberOfDecimal "roundAfter:" | number | number := 10 raisedTo: aNumberOfDecimal. ^(self * 10^aNumber) truncated "rounded" / 10^aNumber asFloat
and still MethodFinder methodFor: #(#(1.2288 3) 1.228) returns '(no single method does that function) ' whereas 1.2288 roundedAfter: 3 returns 1.229
Does anybody know what was wrong in my method finder use ?
Thanks
Cédrick
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Try with
MethodFinder methodFor: #(#(1001.2288 0.001) 1001.228).
It works even if (1001.2288 truncateTo: 0.001) ~= 1001.228, because of inexact arithmetic (in fact, it may depends which library is reading the Float; results are different if above decimal representation are rounded to nearest Float as SqNumberParser does).
The magic behind is that MethodFinder uses closeTo: rather than =, and this is right because no one should rely on Float equality after performing some inexact operations.
Nicolas
beginners@lists.squeakfoundation.org