Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-10

format function

25 messages · 11 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrea Mauri

Dear all,
this is my first mail to this mailing list, I'm not sure this is the correct one to post this topic.
I noticed a strange behavior of the format function. I'm not sure it can be considered as a bug.
If I use
format('%.3f', [-0.0001])
the resulting string is
-0.000
and not
0.000
Why?

Andrea

Reply by email

Vinzent Hoefler
to Andrea Mauri
Andrea Mauri wrote 12 lines
Dear all, this is my first mail to this mailing list, I'm not sure this is the correct one to post this topic. I noticed a strange behavior of the format function. I'm not sure it can be considered as a bug. If I use format('%.3f', [-0.0001]) the resulting string is -0.000 and not 0.000 Why?

Hmm, I'd guess that's because the (original) number is negative and the sign is not changed by rounding or chopping off digits.

Vinzent.

Reply by email

Michael Van Canneyt
to Andrea Mauri: this is my first mail to this mailing list, I'm not sure this is the correct one to post…
Andrea Mauri wrote 12 lines
Dear all, this is my first mail to this mailing list, I'm not sure this is the correct one to post this topic. I noticed a strange behavior of the format function. I'm not sure it can be considered as a bug. If I use format('%.3f', [-0.0001]) the resulting string is -0.000 and not 0.000 Why?

Because your number is negative... The original number is taken to decide whether it should put the - sign or not. What does delphi do ?

Michael.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 14 lines
> I noticed a strange behavior of the format function. I'm not sure > it can be > considered as a bug. > If I use > format('%.3f', [-0.0001]) > the resulting string is > -0.000 > and not > 0.000 > Why? Because your number is negative... The original number is taken to decide whether it should put the - sign or not. What does delphi do ?

Not only that, but -0.000 is different from 0.000 (at least for the fpu)

Jonas

Reply by email

Florian Klaempfl
to Andrea Mauri
Andrea Mauri wrote 7 lines
Jonas Maebe wrote: > > Not only that, but -0.000 is different from 0.000 (at least for the fpu) > > > Jonas In a mathematical way of view I think that -0.000 has no meaning.

Well, despite the fact that -0.000 is probably indeed wrong in this case, a mathematical view is often wrong when using an fpu :)

Show quoted text 6 lines
If I round something and the resulting value is zero, well zero is zero. Not +0 or -0. a. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Пётр Косаревский
Show quoted text 8 lines
>> Not only that, but -0.000 is different from 0.000 (at least for the fpu) >> Jonas > In a mathematical way of view I think that -0.000 has no meaning. Well, despite the fact that -0.000 is probably indeed wrong in this case, a mathematical view is often wrong when using an fpu :) > If I round something and the resulting value is zero, well zero is zero. > Not +0 or -0. > a.

It's a matter of tradition. If FPC is to behave like Delphi, the "question" is definitely a bug report.

In the othe case, if the user is supposed to understand, that he looks at some representation of binary representation of a real number, "-0.000" obviously means "little negative number". And user may understand, that multiplying it by 10000 he is not likely to get positive number. And it has nothing in common with FPU plus/minus zeroes in the example.

I personally prefer "-0.000" in this case.

Reply by email

Jonas Maebe
to Пётр Косаревский
Пётр Косаревский wrote
In the othe case, if the user is supposed to understand, that he looks at some representation of binary representation of a real number, "-0.000" obviously means "little negative number".

No, it means the value 0, but with a negative sign. The value "-0" is representable in an exact way on any IEEE standard-compliant fpu. The reason is that fpu's have a separate bit to store the sign. So even if the mantissa and exponent show that it's the value zero, the sign bit can still be one (meaning that it's a negative zero). And for some fpu calculations this even matters.

Jonas

Reply by email

Christian Ulrich
to Jonas Maebe: No, it means the value 0, but with a negative sign. The value "-0" is representable in an…

when the statement

if (-0 = 0)

is true then RoundTo schould not return an numer like -0 ?! i think the point of view of an fpu is in the round functions not nessesary 0 is 0 and not -0

Earlier message 25 lines
----- Original Message ----- From: "Jonas Maebe" To: "FPC developers' list" Sent: Thursday, October 12, 2006 3:46 PM Subject: Re: Re[2]: [fpc-devel] format function On 12 okt 2006, at 15:41, Пётр Косаревский wrote: > In the othe case, if the user is supposed to understand, that he looks at > some representation of binary representation of a real number, "-0.000" > obviously means "little negative number". No, it means the value 0, but with a negative sign. The value "-0" is representable in an exact way on any IEEE standard-compliant fpu. The reason is that fpu's have a separate bit to store the sign. So even if the mantissa and exponent show that it's the value zero, the sign bit can still be one (meaning that it's a negative zero). And for some fpu calculations this even matters. Jonas_______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Пётр Косаревский
representable in an exact way on any IEEE standard-compliant fpu. The

Of course. But the result of "format" is user-oriented. User is not supposed to know about negative zero, denormal numbers, negative infinity and the whole IEEE 754.

If the user is supposed to understand that "-0.000" means "a small negative number", then it's ok. If the user will be terrified, it would be bad.

Reply by email

Daniël Mantione
to Пётр Косаревский: Of course. But the result of "format" is user-oriented. User is not supposed to know…

Op Thu, 12 Oct 2006, schreef ???? ???????????:

Show quoted text 5 lines
> representable in an exact way on any IEEE standard-compliant fpu. The Of course. But the result of "format" is user-oriented. User is not supposed to know about negative zero, denormal numbers, negative infinity and the whole IEEE 754. If the user is supposed to understand that "-0.000" means "a small negative number", then it's ok. If the user will be terrified, it would be bad.

Grandma is not the only user of a computer. A computer is a precision instrument, usable by scientists. Format is used to display numbers in scientific notation.

0 means "somewhere between 0 and 2^-127".
-0 means "somewhere between -2^-127 and 0".

The difference is of scientific meaning. If there were no negative zero, the precision around zero would be halved.

Daniël

Reply by email

Jonas Maebe
to Micha Nelissen: And not answering the question ... sigh. As shown there, you never notice the difference…
Micha Nelissen wrote 7 lines
Michael Van Canneyt wrote: >>> Like ? >> http://en.wikipedia.org/wiki/Negative_zero > Jonas being succinct as ever :) And not answering the question ... sigh. As shown there, you never notice the difference in result.

I don't understand what you mean here. I said that for some fpu calculations the sign of zero matters. I understood that you asked for examples when you answered with "Like?". I therefore gave a link to a wikipedia article which shows that the sign matters e.g. when dividing by zero (positive or negative infinity), and the article also gives some examples of real-world cases where the sign of zero gives a different meaning to the result.

What else did you expect?

Jonas

Reply by email

Micha Nelissen
to Jonas Maebe: I don't understand what you mean here. I said that for some fpu calculations the sign of…
Jonas Maebe wrote
examples when you answered with "Like?". I therefore gave a link to a wikipedia article which shows that the sign matters e.g. when dividing by zero (positive or negative infinity), and the article also gives some

Well I meant a "real practical" difference, not something at the extremes. Anything done with infinity is infinity or zero, so that's not a "noticeable" difference IMHO.

Micha

Reply by email

Пётр Косаревский
Why do you think that scientific notation is used by default? > I personally prefer "-0.000" in this case.

It's just a personal preference.

A weak argument:
If you write a program "grandma" should use, you can always add an extra check if she complains (and it will be only one of the many "extra checks"!). If you want the program to be more rough and scientist oriented, it will be one of only few "extra checks".

Reply by email

Rimgaudas Laucius
to Пётр Косаревский

Maybe this is off topic, but some interesting information about floating point operations could be found at
http://vod.niif.hu/index.php?lg=en&mn=archive&eid=47&sm=listevent&secid=85. I recommend you to check "Can we trust floating-point numbers?" by Paul Zimmermann.
ISO/IEC 10967-3 "Information technology -- Language independent arithmetic -- Part 3: Complex integer and floating point arithmetic and complex elementary numerical functions" is worth to check too.

Earlier message 21 lines
----- Original Message ----- From: "Пётр Косаревский" To: "FPC developers' list" Sent: Thursday, October 12, 2006 5:51 PM Subject: Re[3]: [fpc-devel] format function >> Why do you think that scientific notation is used by default? >>> I personally prefer "-0.000" in this case. > > It's just a personal preference. > > A weak argument: > If you write a program "grandma" should use, you can always add an extra > check if she complains (and it will be only one of the many "extra > checks"!). If you want the program to be more rough and scientist > oriented, it will be one of only few "extra checks". > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel >

Reply by email

John Briggs
Florian Klaempfl wrote 8 lines
Andrea Mauri wrote: > Jonas Maebe wrote: >> >> Not only that, but -0.000 is different from 0.000 (at least for the fpu) >> >> >> Jonas > In a mathematical way of view I think that -0.000 has no meaning.

It does it means that the original number was negative before rounding.
0 is neither positive or negative but -0.000 is both correct and meaningful. The precision (4 digits) indicates that it is not exactly 0 but is slightly negative ie:

x = -0.000 ==> x < 0.000 and x > -0.0005 ( rounding towards zero )

where

x = 0.000 ==> x > 0.000 and x < 0.0005 ( rounding towards zero )

It is the implied precision that is causing you the problems. Besides the other problem is that converting decimal -1E-4 to binary results in an irrational binary number ( similar to 1/3 in decimal).

As long as you use floating point numbers in normal computing tasks you need to be aware of the implied precision problems and do error checking in your code.

John

Show quoted text 7 lines
Well, despite the fact that -0.000 is probably indeed wrong in this case, a mathematical view is often wrong when using an fpu :) > If I round something and the resulting value is zero, well zero is zero. > Not +0 or -0. > a.

Reply by email