Formatting -0.0 as 0.0

Bastiaan Veelo Bastiaan at Veelo.net
Fri Oct 15 15:18:45 UTC 2021


Thank you Steve.

On Friday, 15 October 2021 at 14:09:32 UTC, Steven Schveighoffer 
wrote:
> On 10/15/21 8:37 AM, Bastiaan Veelo wrote:
>
>> What I would like best is if there were a format specifier 
>> flag that does this conversion automatically, like "%> 4.3f". 
>> Does it make sense to add this? Is there a better way?
>
> The only way I can think of that isn't really intrusive (like 
> your `posZero` thing) is to define your own `writef`/`format` 
> wrappers (what I'd do is check any doubles, and replace them 
> when necessary).

The thing that is holding me back is that it feels backwards to 
wrap `std` to fix what I perceive as a usability issue, contra 
fixing `std` itself. Hence my proposal of extending the format 
specification -- although I find it complicated enough as it is. 
I am surprised that I am the first to have this problem.

I think I'm going to follow your suggestion nonetheless, as 
seeing `posZero` all over the place looks ridiculous...

> Another thing to do is to catch it at the source. That is, when 
> you store a value somewhere where it might be printed, if it's 
> `-0.0`, change it to `0.0`.

Is not an option in my situation, as it must work on machine 
translated code. For new code it would work, but I doubt our 
programmers will be conscious of the issue at all times. As we 
work with a left-handed coordinate system, multiplying Y with -1 
happens frequently, and 0 is a common value for Y. Easy to miss a 
spot.

> Note that if ldc is optimizing out an addition with 0, and that 
> actually changes the observable results, that technically is an 
> invalid optimization.

Issue filed: https://github.com/ldc-developers/ldc/issues/3851

Fun fact: I checked two other Pascal compilers: gpc prints `-0.0` 
as `0.0` like our old compiler does, fpc is like D and prints it 
as `-0.0`.

--Bastiaan.





More information about the Digitalmars-d mailing list