Replacement for snprintf

Rumbu rumbu at rumbu.ro
Wed Oct 30 20:29:34 UTC 2019


On Wednesday, 30 October 2019 at 19:28:44 UTC, berni44 wrote:
> On Wednesday, 30 October 2019 at 18:16:56 UTC, Rumbu wrote:
>> That means that snprintf must use the current rounding mode 
>> that can be read using FloatingPointControl.rounding from 
>> std.math.
>
> Is it really a "must"? We are not completely bound by the IEEE 
> standard and, if good reasons are available, might reject it. 
> For example, comparing two floats with <= produces either 
> "false" or "true" in D. According to IEEE there should be a 
> third result possible, namly "not comparable". Having said 
> this, it would be possible to implement it the way you claim, 
> but probably at some cost (=slower, more and less easy readable 
> lines of code). I'll think about it.

I don't know the inners of your code, but I suppose that before 
"printing" you end up with an integer value and a ten-based 
exponent.

In this case rounding becomes a question of how do you interpret 
the remainder of a division by a power of ten.

Because I spent a lot of time figuring out how  to format 
correctly decimal numbers, here is some piece of code I use in 
order to format decimal numbers depending on the rounding mode, 
fully compliant with the standard.

https://github.com/rumbu13/decimal/blob/a6bae32d75d56be16e82d37af0c8e4a7c08e318a/src/decimal/decimal.d#L7296

Hope this helps.






More information about the Digitalmars-d mailing list