# why does 69.99*100=6998? - Page 2

•  Subject
• Author
• Posted on

## Re: why does 69.99*100=6998?

it's simply a rounding error! because type float is weak while storing
the numbers with floating points you should store values using type
double or you can divide each part of the number ex. for 89.98 as 89
and 98  you could do this using php's bundled string manipulation
functions..

## Re: why does 69.99*100=6998?

Betikci Boris wrote:

No,what it is is a fundamental flaw in the thinking of whoever designed
the language, to consider that converting from a float to an integer
would be a matter of TRUNCATION rather than rounding.

## Re: why does 69.99*100=6998?

No. What it is is a fundamental flaw in your understanding of computer
hardware, particularly the floating point arithmetic unit.

What you see are artifacts fundamental to floating point, not any specific
language.

--
Richard.

## Re: why does 69.99*100=6998?

rf wrote:

Don't be silly.

You have a number, that as far as the programmer is concerned is 69.99

It is represented internally as 69.989999999999 or some such.

Your software multiples it by 100 and it gets to be 6998.999999999

You need to turn it into an integer.

Now what is the most logical thing to do?

round to nearest? or chop off the whole rear end?

Especially when two functions to print, turn up different resuklts.

i.e.

printf("%s %d", 69.99*100, 69.99*100); gives you two different results?

Why isn't the "%s" giving 6998.9999999?
I'ts fairly obvious that its doing some sort of rounding, there, just
NOT in the %d case..

## Re: why does 69.99*100=6998?

I am not being silly. I am just reporting what the most used processor chip
used these days does. It truncates. Live with it, or get a job far far away
from computers.

OK so far.

Correct. A limitation of floating point. Base 2 floating point cannot
exactly represent the number you have.

Correct. However it is not *my* software. It is the processor that *you* are
using. Processor, not software.

Do I? No, sir, I do not. I keep all my monetory values in cents. Only when I
print them out do I turn them into a decimal. This is what you should do as
well.

just about every computer/chip manufacturer out there, IBM, CRAY, all of
them: it gets truncated.

Whatever.

Get this through your thick head: Floating point is *NOT* accurate when you
are talking about non-integers. This is quite well known throuhout the
entire IT industry.

--
Richard.

## Re: why does 69.99*100=6998?

rf wrote:

Well, leaving aside its pence here, not cents,thats what I DO.

However someone at some point expects to input £69.99, and that needs to
go into a database as 6999.. so its reasonably logical to assume that a
sprintf statement taking that value*100 and putting it into an integer
would work..

This is not done at chip level.

This is done within the printf function in PHP.

I have known it for years, however its GENERAL to assume that if there
are *IMPLICIT* floating point to integer casts going on, its a round,
not a trunc that gets used.

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

YOU expect it to.  Knowledgeable programmers understand what really happens.

Actually, it is.

Which conversion is performed at the chip level by a few machine
language instructions.

Implicit floating point to integer casts truncate in every language.
Pretty consistent.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: why does 69.99*100=6998?

Jerry Stuckle wrote:

[putolin]

Actually it isn't at the chip level,  A consequence of the binary numbering
system.

If the current microprocessors were built on the decimal numbering system
then it would matter would it.

--
Dancin in the ruins tonight
Tayo'y Mga Pinoy

## Re: why does 69.99*100=6998?

Baho Utot wrote:

Once more, Baho, you open your mouth and remove all doubt about your
stupidity.

Current processors (from PC's to mainframes) handle floating point
numbers on the chip - they do all 4 math functions.  They also convert
between integer and floating point.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: why does 69.99*100=6998?

Jerry Stuckle wrote:

Once again you don't understand what you read.

It makes no difference if it is handle by a chip or not.

The problem manifests itself from the binary numbering system...not a piece
of silicon.  You are talking implementation, I am talking the real cause.

--
Dancin in the ruins tonight
Tayo'y Mga Pinoy

## Re: why does 69.99*100=6998?

Baho Utot wrote:

Oh, I understand what I read.

Sure it does.  But as always, you're too stoopid to understand anything
which requires a modicum of intelligence.

Not at all.  You have no idea what you're talking about.  But that's
nothing new, Baho.  You seldom do.

But you're still willing top open your mouth and show everyone what an
idiot you are.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: why does 69.99*100=6998?

When dealing with floating point you will only get a 'very close'
http://docs.python.org/tut/node16.html
does an excellent job of explaining it. Don't be concerned that this came
off the Python website, it still applies in this case.

http://www2.hursley.ibm.com/decimal/decifaq.html

Daniel Klein

## Re: why does 69.99*100=6998?

Daniel Klein wrote:

My issue is not with binary mathematics, chips or how computers work.
It is solely with the way PHP handles casts from floats to integers.

## Re: why does 69.99*100=6998?

If you bothered to follow the suggestions that have been made to you,
do the rounding ourselves. I've been doing the equivalent of this:

\$int = \$float + 0.5;

since about 1966, in a wide variety of languages.

## Re: why does 69.99*100=6998?

Sorry. I thought this information would be helpful to the group in general.
I was not responding to a specific point, hence the large 'snipped' section.

I will take more care next time in the placement of my replies.

Apologies,
Daniel Klein

[ps: Is this type of reply not appropriate for this forum?]

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

Nope.  Well documented.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

How does the language know that you want your result rounded. You are
failing to understand that even though your input is (string)69.99, the
second you convert it to a float you are losing precision. PHP doesn't
know what your intentions are with the data. PHP has no way of knowing
that you want a rounded result unless you tell it so... nor does it have
any way of knowing that the original value was 69.99.

round(\$float [,\$precision])

check round function on php.net for a lot of examples.

--
Norman
Registered Linux user #461062

## Re: why does 69.99*100=6998?

Norman Peelman wrote:

How does it know I want it truncated?

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

It doesn't. It assumes that's what you want because you haven't told
it to do an explicit round(). Why doesn't the echo or print commands
automatically put '\n\r' after each line of output? Surely that's what
you want, right? Probably not. Languages don't know what you are doing
with the variables/output unless you tell them to do specific things.

I suggest you look it up on php.net and follow the examples of all
the other people that have learned how to deal with this. There are a
lot of great examples.

--
Norman
Registered Linux user #461062

## Re: why does 69.99*100=6998?

You are. Instead of reading in the user's input as 69.99, you should be
parsing it yourself into two components, the pounds and the pence. That
would give you:

\$pounds = 69
\$pence  = 99

Then you have your internal value:

\$uservalue = 100*\$pounds + \$pence;

which will always be accurate.

How do you do that parsing? Look at PHP's string functions.

That's what I would do, anyway, instead of bleating on about floating
point.