[Lazarus] formatfloat negative zero (-0)

Andrea Mauri andrea.mauri.75 at gmail.com
Mon Jul 7 15:00:16 CEST 2014


>>
>> using formatfloat function I got an unexpected behaviour.
>> If I round negative numbers I can get as result of formatfloat = '-0'.
>> I think it is a bug, since -0 has no meaning, zero is zero.
>> How can I make a workaround?
>> e.g.
>> label1.Caption:= FormatFloat('0.###', strtofloat(edit1.Text));
>> if in edit1.text I have -0.000001 I got -0 as result while I would like to get 0.
>>
>> Is it a bug, what can I do?
>
> Minus-zero is a recognized computational 'feature' of floats on computers, not a bug (AFAIK).
>
> The float variable value of StrToFloat(edit1.text) is still -0.000001 though;
> (try assigning it to a float type variable and watch it)

OK, I understand that computationally -0 has meaning (since as you said 
the float variable value of StrToFloat(edit1.text) is still -0.000001).
Anyway It has no meaning for the final user of the application (at least 
for any standard user of an application).
The final user does not care that -0 has meaning for the computer.
e.g. I would like to represent a float value with 3 decimal digits. I 
don't care of the remaining digits, -0.000 has no meaning, it just means 
that there are some digits (somewhere after the third decimal digits) 
that are not equal to zero and that if they are considered the value is 
negative (but I don't want to consider any digit after the third decimal 
one!).
Try to do it in excel or libreoffice:
- write in a cell -0.00001 and set to 3 the decimal digits, you got 
-0.000 or 0.000?
And you got the same results using any software that deals with numbers..

>
> Does the same happen if you Round() it to integer?
> e.g.
> label1.caption := Format('%d',[Round(strtofloat(edit1.text))]);
> (I know this isn't what you're after

In this way I got 0.
Anyway I used as example formatfloat since it has the same behaviour of 
TFloatField.DisplayFormat, additionally DisplayFormat is a property that 
is used to represent a formatted value to the final user (a human being 
not a pc) for whom -0 has no meaning.

A.

>
>>
>> Best regards,
>> Andrea Mauri
>>
> -L.
>
>
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>




More information about the Lazarus mailing list