[Newbies] How to round a float?
7 / 8.0 roundTo: 2 I expect 0.88, I get 0 What am I doing wrong, or this a bug? TIA Chuck ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
[Newbies] How to round a float?
Hi Guys, On Wed, 4 Feb 2015 08:22:02 -0500, Johann Hibschman wrote: >That just looks like floating-point representation. It's as rounded as it >can get! >-Johann At first I thought some more parans "(" were needed but that didn't change anything. No this sounds like a bug to me (ar at least something that can be improved) as VA Smalltalk prints 162.89, so somehow they are able to make the floats do the right thing. Lou > >On Wed, Feb 4, 2015 at 6:22 AM, Chuck Hipschman >wrote: > >> 7 / 8.0 roundTo 0.01 = 0.88 Aha! Thanks! >> >> But: >> >> 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.890001 >> >> "162.88946267774418" "unrounded" >> 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.890001 >> >> "207.8928179411367" "unrounded" >> Bug, or me again :-) >> >> MacBook Pro (Retina, 15-inch, Early 2013) System Version: OS X 10.10.2 >> (14C109) >> >> Image >> - >> >> /Users/chuck/Downloads/Squeak-4.5-All-In-One/Squeak-4.5-All-in-One.app/Contents/Resources/Squeak4.5-13680.image >> Squeak4.5 >> latest update: #13680 >> Current Change Set: Unnamed1 >> Image format 6505 (32 bit) >> >> Virtual Machine >> --- >> >> /Users/chuck/Downloads/Squeak-4.5-All-In-One/Squeak-4.5-All-in-One.app/Contents/MacOS/Squeak >> Croquet Closure Cog VM [CoInterpreter VMMaker.oscog-eem.331] 4.5 >> Mac OS X built on Aug 22 2013 10:08:05 Compiler: 4.2.1 (Apple Inc. build >> 5666) (dot 3) >> platform sources revision VM: r2776 >> http://www.squeakvm.org/svn/squeak/branches/Cog Plugins: r2545 >> http://squeakvm.org/svn/squeak/trunk/platforms/Cross/plugins >> CoInterpreter VMMaker.oscog-eem.331 uuid: >> 37d2e4b0-2f37-4e2d-8313-c63637785e59 Aug 22 2013 >> StackToRegisterMappingCogit VMMaker.oscog-eem.333 uuid: >> 84da9cb8-7f30-4cb7-b4fb-239a11f63b54 Aug 22 2013 >> >> On Wed, Feb 4, 2015 at 2:59 AM, Ben Coman wrote: >> >>> In the interest of teaching how to fish, look at the senders of #roundTo: >>> to find its usage. >>> >>> I'll post a full answer tomorrow if you don't beat me to it. >>> >>> cheers -ben >>> >>> On Wed, Feb 4, 2015 at 4:34 PM, Chuck Hipschman >>> wrote: >>> 7 / 8.0 roundTo: 2 I expect 0.88, I get 0 What am I doing wrong, or this a bug? TIA Chuck ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners >>> >>> ___ >>> Beginners mailing list >>> Beginners@lists.squeakfoundation.org >>> http://lists.squeakfoundation.org/mailman/listinfo/beginners >>> >>> >> >> ___ >> Beginners mailing list >> Beginners@lists.squeakfoundation.org >> http://lists.squeakfoundation.org/mailman/listinfo/beginners >> >> --- Louis LaBrunda Keystone Software Corp. SkypeMe callto://PhotonDemon mailto:l...@keystone-software.com http://www.Keystone-Software.com ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
[Newbies] How to round a float?
Hi Nicolas, >Lou, I'm very suspicious about what VA does. >IMO you should not trust too much what you see (what it prints!) I think I agree with everything you say. I assumed (I know that might get me in trouble) that Chuck was rounding to print or display the value. Personally I don't see any reason to round floats (or almost anything else) except to display them in a simpler form. Given that, 162.89 looks better than 162.890001 and is probably what Chuck was looking for. That's why I thought there was a problem with #roundTo: but maybe #roundTo: has some other intent that I'm not aware of? Lou --- Louis LaBrunda Keystone Software Corp. SkypeMe callto://PhotonDemon mailto:l...@keystone-software.com http://www.Keystone-Software.com ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
[Newbies] How to round a float?
Hi Bert, >> That's why I thought there was a problem with #roundTo: but maybe #roundTo: >> has some other intent that I'm not aware of? > >It's used for snapping values to a grid. E.g. in ScrollBar>>setValue:. >- Bert - Ahh, Thanks Bert. So it isn't exactly the same as displaying the numbers but close. And in that case I guess a value like 162.890001 is good enough and there wouldn't be any growing error as each snap brings the value back where you want it. Lou --- Louis LaBrunda Keystone Software Corp. SkypeMe callto://PhotonDemon mailto:l...@keystone-software.com http://www.Keystone-Software.com ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] How to round a float?
In the interest of teaching how to fish, look at the senders of #roundTo: to find its usage. I'll post a full answer tomorrow if you don't beat me to it. cheers -ben On Wed, Feb 4, 2015 at 4:34 PM, Chuck Hipschman wrote: > 7 / 8.0 roundTo: 2 I expect 0.88, I get 0 > > What am I doing wrong, or this a bug? > > TIA > > Chuck > > ___ > Beginners mailing list > Beginners@lists.squeakfoundation.org > http://lists.squeakfoundation.org/mailman/listinfo/beginners > > ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] How to round a float?
7 / 8.0 roundTo 0.01 = 0.88 Aha! Thanks! But: 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.890001 "162.88946267774418" "unrounded" 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.890001 "207.8928179411367" "unrounded" Bug, or me again :-) MacBook Pro (Retina, 15-inch, Early 2013) System Version: OS X 10.10.2 (14C109) Image - /Users/chuck/Downloads/Squeak-4.5-All-In-One/Squeak-4.5-All-in-One.app/Contents/Resources/Squeak4.5-13680.image Squeak4.5 latest update: #13680 Current Change Set: Unnamed1 Image format 6505 (32 bit) Virtual Machine --- /Users/chuck/Downloads/Squeak-4.5-All-In-One/Squeak-4.5-All-in-One.app/Contents/MacOS/Squeak Croquet Closure Cog VM [CoInterpreter VMMaker.oscog-eem.331] 4.5 Mac OS X built on Aug 22 2013 10:08:05 Compiler: 4.2.1 (Apple Inc. build 5666) (dot 3) platform sources revision VM: r2776 http://www.squeakvm.org/svn/squeak/branches/Cog Plugins: r2545 http://squeakvm.org/svn/squeak/trunk/platforms/Cross/plugins CoInterpreter VMMaker.oscog-eem.331 uuid: 37d2e4b0-2f37-4e2d-8313-c63637785e59 Aug 22 2013 StackToRegisterMappingCogit VMMaker.oscog-eem.333 uuid: 84da9cb8-7f30-4cb7-b4fb-239a11f63b54 Aug 22 2013 On Wed, Feb 4, 2015 at 2:59 AM, Ben Coman wrote: > In the interest of teaching how to fish, look at the senders of #roundTo: > to find its usage. > > I'll post a full answer tomorrow if you don't beat me to it. > > cheers -ben > > On Wed, Feb 4, 2015 at 4:34 PM, Chuck Hipschman > wrote: > >> 7 / 8.0 roundTo: 2 I expect 0.88, I get 0 >> >> What am I doing wrong, or this a bug? >> >> TIA >> >> Chuck >> >> ___ >> Beginners mailing list >> Beginners@lists.squeakfoundation.org >> http://lists.squeakfoundation.org/mailman/listinfo/beginners >> >> > > ___ > Beginners mailing list > Beginners@lists.squeakfoundation.org > http://lists.squeakfoundation.org/mailman/listinfo/beginners > > ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] How to round a float?
Hi, 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.890001 "162.88946267774418" "unrounded" 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.890001 "207.8928179411367" "unrounded" this is to be expected and I think you need a bigger pond than the Squeak image to fish for that. I think I found the hint inside Squeak only because I know the reason. I think you did your fishing well. I'll explain later because maybe I can learn better fishing from Ben's reply. Cheers, Herbert ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] How to round a float?
That just looks like floating-point representation. It's as rounded as it can get! -Johann On Wed, Feb 4, 2015 at 6:22 AM, Chuck Hipschman wrote: > 7 / 8.0 roundTo 0.01 = 0.88 Aha! Thanks! > > But: > > 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.890001 > > "162.88946267774418" "unrounded" > 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.890001 > > "207.8928179411367" "unrounded" > Bug, or me again :-) > > MacBook Pro (Retina, 15-inch, Early 2013) System Version: OS X 10.10.2 > (14C109) > > Image > - > > /Users/chuck/Downloads/Squeak-4.5-All-In-One/Squeak-4.5-All-in-One.app/Contents/Resources/Squeak4.5-13680.image > Squeak4.5 > latest update: #13680 > Current Change Set: Unnamed1 > Image format 6505 (32 bit) > > Virtual Machine > --- > > /Users/chuck/Downloads/Squeak-4.5-All-In-One/Squeak-4.5-All-in-One.app/Contents/MacOS/Squeak > Croquet Closure Cog VM [CoInterpreter VMMaker.oscog-eem.331] 4.5 > Mac OS X built on Aug 22 2013 10:08:05 Compiler: 4.2.1 (Apple Inc. build > 5666) (dot 3) > platform sources revision VM: r2776 > http://www.squeakvm.org/svn/squeak/branches/Cog Plugins: r2545 > http://squeakvm.org/svn/squeak/trunk/platforms/Cross/plugins > CoInterpreter VMMaker.oscog-eem.331 uuid: > 37d2e4b0-2f37-4e2d-8313-c63637785e59 Aug 22 2013 > StackToRegisterMappingCogit VMMaker.oscog-eem.333 uuid: > 84da9cb8-7f30-4cb7-b4fb-239a11f63b54 Aug 22 2013 > > On Wed, Feb 4, 2015 at 2:59 AM, Ben Coman wrote: > >> In the interest of teaching how to fish, look at the senders of #roundTo: >> to find its usage. >> >> I'll post a full answer tomorrow if you don't beat me to it. >> >> cheers -ben >> >> On Wed, Feb 4, 2015 at 4:34 PM, Chuck Hipschman >> wrote: >> >>> 7 / 8.0 roundTo: 2 I expect 0.88, I get 0 >>> >>> What am I doing wrong, or this a bug? >>> >>> TIA >>> >>> Chuck >>> >>> ___ >>> Beginners mailing list >>> Beginners@lists.squeakfoundation.org >>> http://lists.squeakfoundation.org/mailman/listinfo/beginners >>> >>> >> >> ___ >> Beginners mailing list >> Beginners@lists.squeakfoundation.org >> http://lists.squeakfoundation.org/mailman/listinfo/beginners >> >> > > ___ > Beginners mailing list > Beginners@lists.squeakfoundation.org > http://lists.squeakfoundation.org/mailman/listinfo/beginners > > ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] How to round a float?
On Wed, Feb 4, 2015 at 7:22 PM, Chuck Hipschman wrote: > 7 / 8.0 roundTo 0.01 = 0.88 Aha! Thanks! > > But: > > 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.890001 > > "162.88946267774418" "unrounded" > 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.890001 > > "207.8928179411367" "unrounded" > Bug, or me again :-) > > If you debug into #roundTo: you'll see its implementation is simple arithmetic, subject to the vagaries of float resolution. Float(Number)>>roundTo: ^(self / quantum) rounded * quantum Now are you wanting something nicely formatted for display, or are you rounding for some mathematic purpose? Perhaps instead you are wanting... 100 * (1.05 raisedTo: 10) printShowingDecimalPlaces: 2 " --> 162.89 " 0.105 printShowingDecimalPlaces: 2" --> 0.10 " 0.115 printShowingDecimalPlaces: 2" --> 0.12 " btw, I only found these today, looking in the printing protocol of Number and Float. cheers -ben ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] How to round a float?
On 16.02.2015, at 07:29, Louis LaBrunda wrote: > > That's why I thought there was a problem with #roundTo: but maybe #roundTo: > has some other intent that I'm not aware of? It's used for snapping values to a grid. E.g. in ScrollBar>>setValue:. - Bert - smime.p7s Description: S/MIME cryptographic signature ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners