How to round a float?

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

How to round a float?

Chuck Hipschman
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Ben Coman
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 <[hidden email]> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Chuck Hipschman
7 / 8.0 roundTo 0.01 = 0.88  Aha! Thanks!

But:

100 * (1.05 raisedTo: 10) roundTo: 0.01 162.89000000000001 
                                                              "162.88946267774418" "unrounded"
100 * (1.05 raisedTo: 15) roundTo: 0.01 207.89000000000001
                                                               "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)
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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Herbert König
Hi,

> 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.89000000000001
>   "162.88946267774418" "unrounded"
> 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.89000000000001
>    "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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Johann Hibschman
In reply to this post by Chuck Hipschman
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 <[hidden email]> wrote:
7 / 8.0 roundTo 0.01 = 0.88  Aha! Thanks!

But:

100 * (1.05 raisedTo: 10) roundTo: 0.01 162.89000000000001 
                                                              "162.88946267774418" "unrounded"
100 * (1.05 raisedTo: 15) roundTo: 0.01 207.89000000000001
                                                               "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)
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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

How to round a float?

Louis LaBrunda
Hi Guys,

On Wed, 4 Feb 2015 08:22:02 -0500, Johann Hibschman <[hidden email]>
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 <[hidden email]>
>wrote:
>
>> 7 / 8.0 roundTo 0.01 = 0.88  Aha! Thanks!
>>
>> But:
>>
>> 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.89000000000001
>>
>> "162.88946267774418" "unrounded"
>> 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.89000000000001
>>
>>  "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 <[hidden email]> 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 <[hidden email]>
>>> 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
>>>> [hidden email]
>>>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [hidden email]
>>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
>>
-----------------------------------------------------------
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon
mailto:[hidden email] http://www.Keystone-Software.com

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Ben Coman
In reply to this post by Chuck Hipschman


On Wed, Feb 4, 2015 at 7:22 PM, Chuck Hipschman <[hidden email]> wrote:
7 / 8.0 roundTo 0.01 = 0.88  Aha! Thanks!

But:

100 * (1.05 raisedTo: 10) roundTo: 0.01 162.89000000000001 
                                                              "162.88946267774418" "unrounded"
100 * (1.05 raisedTo: 15) roundTo: 0.01 207.89000000000001
                                                               "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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Nicolas Cellier
In reply to this post by Louis LaBrunda
Louis LaBrunda <Lou <at> Keystone-Software.com> writes:

>
> Hi Guys,
>
> On Wed, 4 Feb 2015 08:22:02 -0500, Johann Hibschman <johannh <at> gmail.com>
> 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
>

Lou, I'm very suspicious about what VA does.
IMO you should not trust too much what you see (what it prints!)
Because 16289*0.01 and 162.89 are not really the same float...

(16289*0.01) asFraction printShowingMaxDecimalPlaces: 1074
'162.890000000000014779288903810083866119384765625'

((162.89) asTrueFraction printShowingMaxDecimalPlaces: 1074)
'162.8899999999999863575794734060764312744140625'

For having least surprising read-eval-print loops here are 3 levels of
expectations:
1) every two different Float shall have a different printString;
2) every Float printString shall be reinterpreted to the same Float;
3) every Float shall be printed to the shortest representation that will be
reinterpreted to the same Float.
Squeak responds to level-3, so somehow does the right thing.

I suspect VA has lower expectations (only print the first n decimals, even
if Float are different, they will print the same...).

The question is why rounding to 0.01?

If it's just to print, then it's better to do so with some instructions like
100 * (1.05 raisedTo: 10) printShowingDecimalPlaces: 2.
-> '162.89'

For versatile printing options, I suggest loading NumberPrinter package from
http://ss3.gemstone.com/ss/NumberPrinter/

(FloatPrinter fixed) digitCount: 2; print: 100 * (1.05s2 raisedTo: 10)
-> '162.89'

If it's for monetary things like computing interests, then the advice is to
not use Float but ScaledDecimal.

100 * (1.05s2 raisedTo: 10) roundTo: 0.01s2
-> 162.89s2

Nicolas


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

How to round a float?

Louis LaBrunda
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.89000000000001 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:[hidden email] http://www.Keystone-Software.com

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Bert Freudenberg
On 16.02.2015, at 07:29, Louis LaBrunda <[hidden email]> 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 -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

How to round a float?

Louis LaBrunda
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.89000000000001 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:[hidden email] http://www.Keystone-Software.com

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
cbc
Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

cbc
In reply to this post by Nicolas Cellier
On Fri, Feb 13, 2015 at 4:56 PM, nicolas cellier <[hidden email]> wrote:
...

If it's for monetary things like computing interests, then the advice is to
not use Float but ScaledDecimal.

100 * (1.05s2 raisedTo: 10) roundTo: 0.01s2
-> 162.89s2

Nicolas

So, for money, I would suggest not using ScaledDecimal, but rather FixedDecimal.

ScaledDecimal can do funny things that you would not expect in real money land:

m := 1/3 asScaledDecimal: 2.
m  "==> 0.33s2"
m + m + m "==> 1.00s2"

You shouldn't be allowed to add 33 cents together and get a whole dollar (usd, at least)!

FixedDecimal actually rounds to the specified length (ScaledDecimal keeps the entire precision 'behind the curtains').

m := 1/3 asFixedDecimal: 2.
m  "==> 0.33"
m + m + m "==>  0.99"


-cbc

 

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners