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 [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> 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 ? _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
>
> 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 ? _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Herbert König
Why is it important to you to have a float? Once you've truncated it, you have a rational number, by definition.
Are you sure you want to truncate the number, rather than performing the truncation in the presentation layer? On Wed, Jul 23, 2008 at 10:34 AM, cdrick <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> Why is it important to you to have a float? Once you've truncated it, you
> have a rational number, by definition. > Are you sure you want to truncate the number, rather than performing the > truncation in the presentation layer? In my case, this is only for presentation. The real float doesn't change. And anyway rounded and truncated doesn't change the number value as they are immutable. a := 3.4456. a rounded. a "-> 3.4456" You meant maybe I use ScaledDecimal instead of Float? _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Marcin Tustin
Hello Marcin,
agreed with the the beginning of your post. Floats (their finite precision representation) often create as many problems as they solve, if it weren't for trigonometric functions I'd try to keep away from them as far as I could :-)) MT> Are you sure you want to truncate the number, rather than MT> performing the truncation in the presentation layer? And then Cédrick will go searching the printing protocol of float and find the same problem in a different disguise. Float's private protocol might contain a start with absPrint..... Cheers, Herbert _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by cedreek
Hello Cédrick,
search the "truncating and rounding" and "printing" protocols of Number there you'll find some help. -- Cheers, Herbert _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
arffff thanks all ;)
1.23456 printShowingDecimalPlaces: 3 I knew that had to exist...Since I know the method finder I became lazy to search far ;) See you Cédrick 2008/7/23 Herbert König <[hidden email]>: > Hello Cédrick, > > search the "truncating and rounding" and "printing" protocols of Number > there you'll find some help. > > -- > Cheers, > > Herbert > > _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners > _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by cedreek
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 ? _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> 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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
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. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by cedreek
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 > [hidden email] > 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 mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |