January 11, 2018
On Thursday, 11 January 2018 at 11:19:41 UTC, Robert M. Münch wrote:
> On 2018-01-08 22:16:25 +0000, rumbu said:
>
>> This is my first D finalized project (+16k loc).
>
> Great stuff! Will this work in betterC mode?
>

It will not work without some refactory. Most of phobos/druntime dependencies are minimal an can be rewritten - in fact there are only 9 dependencies: bsr, bsf, addu, subu, adds, subs from druntime and isNaN, isinfinity, signbit from std.math.

The rest of the dependencies are simply traits that must work by default under betterC.

Once dependencies are solved, another problem will be the exception mechanism. 90% of arithmetic operations are meant to throw exceptions, but this can be overridden by the alternate exception handling - raising and setting flags.

BUT - a very big but - the most important issue is the formatting thing. Even there is no direct dependency on std.format or std.stdio, the formatting mechanism is designed to fit nicely into phobos formatting paradigm. That means that there is no way to output decimal values on the console without phobos. I confess that 30% of my development time was the formatting feature: displaying correctly any possible combination of width, padding, alignment in 4 different formats (%f, %g, %e, %a) was really a challenge and despite of hundreds of unit tests, I'm not convinced today that I covered all the corner cases. The f*cking %g took me 3 days to find out exactly what is meant to do. More than that, each compiler producer have a different idea about format specifiers. Linking your application against snn, msvcrt or glibc will render completely different results for printf("%g", ...). On top of that, neither the FormatSpec documentation from phobos is something complete and clear...

AFAIK, there is no way to plug a custom formatting mechanism into printf, not even in plain C (ok, I'm aware that gcc has something in this respect).

To sum things up, it is possible to use it in betterC mode if:
- all dependencies are rewritten;
- the exception handling is dropped out completely;
- a printf equivalent for decimal types is rewritten from scratch or - decimal values must be converted to binary float before printing them - but this will negate the main purpose of the decimal type - precision.




January 11, 2018
On Thursday, 11 January 2018 at 18:15:23 UTC, rumbu wrote:
> [snip]
> BUT - a very big but - the most important issue is the formatting thing. Even there is no direct dependency on std.format or std.stdio, the formatting mechanism is designed to fit nicely into phobos formatting paradigm. That means that there is no way to output decimal values on the console without phobos. I confess that 30% of my development time was the formatting feature: displaying correctly any possible combination of width, padding, alignment in 4 different formats (%f, %g, %e, %a) was really a challenge and despite of hundreds of unit tests, I'm not convinced today that I covered all the corner cases. The f*cking %g took me 3 days to find out exactly what is meant to do. More than that, each compiler producer have a different idea about format specifiers. Linking your application against snn, msvcrt or glibc will render completely different results for printf("%g", ...). On top of that, neither the FormatSpec documentation from phobos is something complete and clear...
>

Ilya has expressed interest in a betterC formatting library for libmir.
January 11, 2018
On 1/11/18 1:15 PM, rumbu wrote:
> On Thursday, 11 January 2018 at 11:19:41 UTC, Robert M. Münch wrote:
>> On 2018-01-08 22:16:25 +0000, rumbu said:
>>
>>> This is my first D finalized project (+16k loc).
>>
>> Great stuff! Will this work in betterC mode?
>>
> 
> It will not work without some refactory. Most of phobos/druntime dependencies are minimal an can be rewritten - in fact there are only 9 dependencies: bsr, bsf, addu, subu, adds, subs from druntime and isNaN, isinfinity, signbit from std.math.

Some of those are intrinsics, and don't actually link to any druntime functions.

-Steve
January 11, 2018
Great project!

On Monday, 8 January 2018 at 22:16:25 UTC, rumbu wrote:
> - all format specifiers implemented (%f, %e, %g, %a);

Really?

[...]

> What's next:
> - more tests;

Here you are:

```
import std.stdio;
import decimal;

void main ()
{
   decimal32 d = "0.7";
   d *= decimal32("1.05");
   d.writeln;
   printf ("%.2f\n", d);

   float f = 0.7f;
   f *= 1.05f;
   f.writeln;
   printf ("%.2f\n", f);

   decimal32 e = 1_000_000_000;
   while (e > 1e-7) {
      e.writeln;
      e /= 10;
   }
}
```

This prints:

   0.735
   0.00      <--- expected: 0.74
   0.735
   0.73
             <--- loop output missing

(DMD64 D Compiler v2.077.1)
January 11, 2018
On Thursday, 11 January 2018 at 20:35:03 UTC, kdevel wrote:
> Great project!
>
> On Monday, 8 January 2018 at 22:16:25 UTC, rumbu wrote:
>> - all format specifiers implemented (%f, %e, %g, %a);
>
> Really?
>
> [...]
>
>> What's next:
>> - more tests;
>
> Here you are:
>
> ```
> import std.stdio;
> import decimal;
>
> void main ()
> {
>    decimal32 d = "0.7";
>    d *= decimal32("1.05");
>    d.writeln;
>    printf ("%.2f\n", d);


C’s printf by definition can’t be customized. What did you expect?

>
>    float f = 0.7f;
>    f *= 1.05f;
>    f.writeln;
>    printf ("%.2f\n", f);
>
>    decimal32 e = 1_000_000_000;
>    while (e > 1e-7) {
>       e.writeln;
>       e /= 10;
>    }
> }
> ```
>
> This prints:
>
>    0.735
>    0.00      <--- expected: 0.74
>    0.735
>    0.73
>              <--- loop output missing
>
> (DMD64 D Compiler v2.077.1)


January 11, 2018
On Thursday, 11 January 2018 at 20:35:03 UTC, kdevel wrote:
>    printf ("%.2f\n", d);

This must read

   writefln ("%.2f", d);

Would have expected a compile time/run time error.

> This prints:
>
>    0.735
>    0.00      <--- expected: 0.74

Good. writefln prints the expected value.

>    0.735
>    0.73
>              <--- loop output missing

This is still missing. If I change the while-loop threshold to 1e-9 the values are printed.
January 11, 2018
On Thursday, 11 January 2018 at 20:40:01 UTC, Dmitry Olshansky wrote:
>>    printf ("%.2f\n", d);
>
> C’s printf by definition can’t be customized.

Sure.
http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html

> What did you expect?

To be honest: A compile time error. Modern C compilers can check such format strings. Example: GCC 6:

mis.c
```
#include <stdio.h>

int main ()
{
   double d = 0;
   printf ("%p\n", d);
   return 0;
}
```

$ gcc -Wall mis.c
mis.c: In function 'main':
mis.c:6:14: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'double' [-Wformat=]
    printf ("%p\n", d);



January 11, 2018
On Thursday, 11 January 2018 at 20:44:13 UTC, kdevel wrote:
> On Thursday, 11 January 2018 at 20:35:03 UTC, kdevel wrote:
>>              <--- loop output missing

loop.d
```
import std.stdio;
import decimal;

void loopme(T) ()
{
   "---".writeln;
   T e = T(1000);
   while (e > T(1e-6)) {
      e.writeln;
      e /= 10;
   }
}

void main ()
{
   loopme!float;
   loopme!decimal32;
}
```

This prints

   ---
   1000
   100
   10
   1
   0.1
   0.01
   0.001
   0.0001
   1e-05
   ---
   1000
   100
   10
   1
   0.1
   0.0100000    <--
   0.00100000   <--
   0.000100000  <--
   1.00000e-05  <--

Why are there trailing zeroes?
January 11, 2018
On 1/11/18 4:12 PM, kdevel wrote:
> On Thursday, 11 January 2018 at 20:40:01 UTC, Dmitry Olshansky wrote:
>> What did you expect?
> 
> To be honest: A compile time error. Modern C compilers can check such format strings. Example: GCC 6:

But dmd isn't a C compiler, nor does it have to worry about the problems C has (namely, untyped varargs). To dmd, printf is just another function, there's nothing special about it.

-Steve
January 11, 2018
On Thursday, 11 January 2018 at 21:12:59 UTC, kdevel wrote:
> On Thursday, 11 January 2018 at 20:40:01 UTC, Dmitry Olshansky wrote:
>>>    printf ("%.2f\n", d);
>>
>> C’s printf by definition can’t be customized.
>
> Sure.
> 

“”” GNU C Library lets you define “””

Here is your compatibility story.

>> What did you expect?
>
> To be honest: A compile time error. Modern C compilers can check such format strings. Example: GCC 6:

1. It’s a warning.
2. There is no typechecking it’s just a hardcoded linting in the compiler.
3. It’s D use writefln or if going for C primitives know their quirks.
4. Passing anything but basic types to C vararg is undefined in C++ IIRC, dunno what D should do about it.

>
> mis.c
> ```
> #include <stdio.h>
>
> int main ()
> {
>    double d = 0;
>    printf ("%p\n", d);
>    return 0;
> }
> ```
>
> $ gcc -Wall mis.c
> mis.c: In function 'main':
> mis.c:6:14: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'double' [-Wformat=]
>     printf ("%p\n", d);