For Programmers: Free Programming Magazines  


Home > Archive > Smalltalk > April 2007 > problem with floating numbers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem with floating numbers
anna21

2007-04-15, 7:08 pm

Hi.
I have to print out floating numbers with 3 digits after the floating
point. I use GNU Smalltalk. Since I culdn't find a function to this,
I was advised to write this function:
roundToDecimals: anInteger
| shift |
shift := 10 raisedTo: anInteger.
^(self * shift) rounded / shift
The problem is that it prints out normal fraction numbers (f.e.
501/200). When I write asFloat after it, it prints 2.50199999999999
while according to my calculator it should be 2.505 (so with 3 digits
as I wanted).
Could you help me how to make it use 3 digits? Thank you very much in
advance.

Cesar Rabak

2007-04-15, 10:06 pm

anna21 escreveu:
> Hi.
> I have to print out floating numbers with 3 digits after the floating
> point. I use GNU Smalltalk. Since I culdn't find a function to this,


Anna,

The method to do this in Smalltalk, and GNU Smalltalk has it, is
Number>>roundTo: aNumber

Where aNumber is a floating point number used as a 'model' of the number
of digits, i.e., is written as (for 3 digits after point) 0.001.

This works as should in other Smalltalks, for example in Squeak or
VisualWorks:
501/200 asFloat roundTo: 0.001

prints 2.505 in the Workspace.

In GNU Smalltalk, however, the commands:

st> 501/200 asFloat roundTo: 0.001 printNl !
0.001
2.50500000000000

Or in the Blox Worksheet:
(501/200 asFloat) roundTo: 0.001

prints 2.50499999999999...

In the second case I think the non rounded value is due the use of
Tcl/Tk as the graphics inteface, and for the command line version, it is
a limitation on how GNU Smalltalk formats a float number in the prompt.


> I was advised to write this function:
> roundToDecimals: anInteger
> | shift |
> shift := 10 raisedTo: anInteger.
> ^(self * shift) rounded / shift
> The problem is that it prints out normal fraction numbers (f.e.
> 501/200). When I write asFloat after it, it prints 2.50199999999999
> while according to my calculator it should be 2.505 (so with 3 digits
> as I wanted).


For this reason, no matter which way you choose to make the calculation,
as soon you as the Float format, it appears again 'wrong'.

However, you can be reassured that internally the value is preserved:
st> Smalltalk at: #a put: 0 !
0
st> a := 501/200 asFloat !
2.50500000000000
st> a printNl !
2.50499999999999
2.50500000000000
st> a asFraction printNl !
501/200
Fraction new "<0xb3d55298>"
st> a inspect !
An instance of FloatD
[1]: 10
[2]: 215
[3]: 163
[4]: 112
[5]: 61
[6]: 10
[7]: 4
[8]: 64
2.50500000000000
st>

HTH

--
Cesar Rabak





> Could you help me how to make it use 3 digits? Thank you very much in
> advance.
>

Paolo Bonzini

2007-04-16, 7:07 pm

On Apr 15, 7:30 pm, "anna21" <anna.p...@vipmail.hu> wrote:
> Hi.
> I have to print out floating numbers with 3 digits after the floating
> point. I use GNU Smalltalk. Since I culdn't find a function to this,
> I was advised to write this function:
> roundToDecimals: anInteger
> | shift |
> shift := 10 raisedTo: anInteger.
> ^(self * shift) rounded / shift
> The problem is that it prints out normal fraction numbers (f.e.
> 501/200). When I write asFloat after it, it prints 2.50199999999999
> while according to my calculator it should be 2.505 (so with 3 digits
> as I wanted).
> Could you help me how to make it use 3 digits? Thank you very much in
> advance.


Hi Anna, you are probably using an old version of GNU Smalltalk.

I suggest you upgrade to GNU Smalltalk 2.3.3 for example (it is now in
Debian unstable too). There, I get

st> (501/200) asFloat printNl!
2.505
2.50500000000000

I also suggest that you subscribe to the mailing list at
http://lists.gnu.org/mailman/listinfo/help-smalltalk where I and other
users are happy to offer our help (comp.lang.smalltalk is also watched
by a few GNU Smalltalk users, though).

Paolo

anna21

2007-04-16, 7:08 pm


Unfortuntely I have to use what's at school :(

Paolo Bonzini

2007-04-16, 7:08 pm

On Apr 16, 11:05 pm, "anna21" <anna.p...@vipmail.hu> wrote:
> Unfortuntely I have to use what's at school :(


Tell them that the GNU Smalltalk maintainer suggests to upgrade. ;-)

Paolo

anna21

2007-04-16, 7:08 pm

I tried this roundto thing. It almost works: f.e. to 5 it still
prints: 5.0000000000000000000... (but it works to other numbers, that
have digits after the point).
But thaks anyway, still better than nothing.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com