Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > floating point issue?

Reply
Thread Tools

floating point issue?

 
 
kevin0051
Guest
Posts: n/a
 
      03-03-2010
I made a perl program as follows.

-----------------
$AAA = 4.31;
$AAA *= 100;
printf ("%f\n", $AAA);
printf ("%d\n", $AAA);
----------------

The output of this program is
431.000000
430

I don't know why the second output is 430 instead of 431.
Can anyone help?

Thanks
Kevin
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      03-03-2010
kevin0051 <(E-Mail Removed)> writes:

> I made a perl program as follows.
>
> -----------------
> $AAA = 4.31;
> $AAA *= 100;
> printf ("%f\n", $AAA);
> printf ("%d\n", $AAA);
> ----------------
>
> The output of this program is
> 431.000000
> 430
>
> I don't know why the second output is 430 instead of 431.
> Can anyone help?


http://en.wikipedia.org/wiki/Floatin...uracy_problems

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
 
Reply With Quote
 
 
 
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      03-03-2010
John Bokma wrote:
> kevin0051 <(E-Mail Removed)> writes:
>
>> I made a perl program as follows.
>>
>> -----------------
>> $AAA = 4.31;
>> $AAA *= 100;
>> printf ("%f\n", $AAA);
>> printf ("%d\n", $AAA);
>> ----------------
>>
>> The output of this program is
>> 431.000000
>> 430
>>
>> I don't know why the second output is 430 instead of 431.
>> Can anyone help?

>
> http://en.wikipedia.org/wiki/Floatin...uracy_problems
>


This surprised me. I knew int() would truncate of course, but I thought
printf with would round in the same way for %d as it does for %.0f,
rather than truncate.

Xho
 
Reply With Quote
 
Steve C
Guest
Posts: n/a
 
      03-03-2010
Xho Jingleheimerschmidt wrote:
> John Bokma wrote:
>> kevin0051 <(E-Mail Removed)> writes:
>>
>>> I made a perl program as follows.
>>>
>>> -----------------
>>> $AAA = 4.31;
>>> $AAA *= 100;
>>> printf ("%f\n", $AAA);
>>> printf ("%d\n", $AAA);
>>> ----------------
>>>
>>> The output of this program is
>>> 431.000000
>>> 430
>>>
>>> I don't know why the second output is 430 instead of 431.
>>> Can anyone help?

>>
>> http://en.wikipedia.org/wiki/Floatin...uracy_problems
>>

>
> This surprised me. I knew int() would truncate of course, but I thought
> printf with would round in the same way for %d as it does for %.0f,
> rather than truncate.
>


It shouldn't surprise you:

perl -e '$f = 4.31; $f *= 100; $h[$f] = 0; print $#h'
430

I would expect any use of a scalar as an integer to truncate.
Similarly:

perl -e '$f = "430plusalittle"; printf "%d",$f'
430

Using a string as an int stops at the first non-digit.
It should not take any of the rest of the string into account.
In the same way, %d should ignore any part of a float other than
the integer part.
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-05-2010
On 2010-03-03 03:25, Tad McClellan <(E-Mail Removed)> wrote:
> kevin0051 <(E-Mail Removed)> wrote:
>> I made a perl program as follows.

>
> You would have the same problem with any programming language.


Except COBOL or PL/SQL or bc or ...


> The problem is related to how numbers are implemented on computers
> rather than how any particular language behaves.


Not "number implemented on computers" in general, but specific
implementations of numbers. Binary floating point numbers are very
common (because almost all modern processors support them in hardware)
but there are many other possible number implementations.

Decimal fixed or floating point numbers avoid the problem for decimal
fractions, which is sufficient for applications which need to represent
decimal fractions exactly (e.g. financial applications), but they still
have the problem for other fractions (1/3, 1/7, ...).

Rational numbers can be stored as a pair of (potentially big) integers.

There is no way to represent all numbers in a finite amount of memory of
course.

The number representation is not completely independent of the language.

C for example mandates that float, double, etc. are floating point
numbers. They can be binary, decimal or use some other base, but they
can't be fractions, for example.

COBOL provides decimal fixed point data types.

PL/SQL has a decimal floating point type (with 38 digits).

bc has arbitrary precision fixed point numbers.

Perl provides several number representations as modules.

hp

 
Reply With Quote
 
sreservoir
Guest
Posts: n/a
 
      03-06-2010
On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
> On 2010-03-03 03:25, Tad McClellan<(E-Mail Removed)> wrote:
>> kevin0051<(E-Mail Removed)> wrote:
>>> I made a perl program as follows.

>>
>> You would have the same problem with any programming language.

>
> Except COBOL or PL/SQL or bc or ...


bc is made for math and few people actually use the other two anymore.

of course, some lisps use fractions.

> [snipped]


> There is no way to represent all numbers in a finite amount of memory of
> course.


there is no way to represent all numbers full stop.

--

"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-06-2010
sreservoir <(E-Mail Removed)> wrote:
>On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
>> There is no way to represent all numbers in a finite amount of memory of
>> course.

>
>there is no way to represent all numbers full stop.


This is becoming philosophical now, but you can represent any number. In
the worst case you simply use a verbal description. Granted, that
doesn't do you much good if you want to use that number in a calculation
on the computer, but it is possible to represent it because otherwise
you wouldn't be able to think of this number in the first place.

jue
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-06-2010
Jürgen Exner wrote:
> sreservoir <(E-Mail Removed)> wrote:
>> On 3/5/2010 5:54 PM, Peter J. Holzer wrote:


>>> There is no way to represent all numbers in a finite amount of memory of
>>> course.

>>
>> there is no way to represent all numbers full stop.

>
> This is becoming philosophical now, but you can represent any number. In
> the worst case you simply use a verbal description. Granted, that
> doesn't do you much good if you want to use that number in a calculation
> on the computer, but it is possible to represent it because otherwise
> you wouldn't be able to think of this number in the first place.


I think you mean that number almost half way between 2 and 3.


--
Ruud
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-06-2010
"Dr.Ruud" <(E-Mail Removed)> wrote:
>Jürgen Exner wrote:
>> sreservoir <(E-Mail Removed)> wrote:
>>> On 3/5/2010 5:54 PM, Peter J. Holzer wrote:

>
>>>> There is no way to represent all numbers in a finite amount of memory of
>>>> course.
>>>
>>> there is no way to represent all numbers full stop.

>>
>> This is becoming philosophical now, but you can represent any number. In
>> the worst case you simply use a verbal description. Granted, that
>> doesn't do you much good if you want to use that number in a calculation
>> on the computer, but it is possible to represent it because otherwise
>> you wouldn't be able to think of this number in the first place.

>
>I think you mean that number almost half way between 2 and 3.
>


No, the other one, the one that is exactly a quarter of an epsilon
smaller.

jue
 
Reply With Quote
 
sreservoir
Guest
Posts: n/a
 
      03-06-2010
On 3/5/2010 8:44 PM, Jürgen Exner wrote:
> "Dr.Ruud"<(E-Mail Removed)> wrote:
>> Jürgen Exner wrote:
>>> sreservoir<(E-Mail Removed)> wrote:
>>>> On 3/5/2010 5:54 PM, Peter J. Holzer wrote:

>>
>>>>> There is no way to represent all numbers in a finite amount of memory of
>>>>> course.
>>>>
>>>> there is no way to represent all numbers full stop.
>>>
>>> This is becoming philosophical now, but you can represent any number. In
>>> the worst case you simply use a verbal description. Granted, that
>>> doesn't do you much good if you want to use that number in a calculation
>>> on the computer, but it is possible to represent it because otherwise
>>> you wouldn't be able to think of this number in the first place.

>>
>> I think you mean that number almost half way between 2 and 3.
>>

>
> No, the other one, the one that is exactly a quarter of an epsilon
> smaller.


nah, it's actually a two ninths of an epsilon.

--

"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
floating point to fixed point conversion riya1012@gmail.com C Programming 4 02-22-2006 05:56 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments