Another take on decimal data types

kdevel kdevel at vogtner.de
Sat Jan 13 17:19:31 UTC 2018


On Saturday, 13 January 2018 at 14:43:53 UTC, rumbu wrote:
> I received a suggestion to reorganize the file structure 
> because of some bug in dub 
> (https://issues.dlang.org/show_bug.cgi?id=11847). The dub.json 
> remained out of sync.
>
> I changed it, but I am not 100% sure that it's working. I am 
> not experienced with dub, If someone wants to maintain 
> dub.json, I will be more than happy to accept any pull request.

I can compile/run the nosine.d now. But implicit conversion 
between decimalX and float/double/real does not seem to work. I 
came across this in

sinus.d
```
import std.stdio;
import std.math;
import decimal;

void main ()
{
    real r;
    for (r = 1; r < 6; r += .1L) {
       decimal128 d = r;
       auto dsin = sin (d);
       auto rsin = sin (r);
       real delta = dsin - rsin;
       writefln ("%9.2f %30.24f %12.4g", r, rsin, dsin, delta);
    }
}
```

$ dmd sinus.d decimal.git/libdecimal.a
sinus.d(12): Error: cannot implicitly convert expression 
dsin.opBinary(rsin) of type Decimal!128 to real

I tried to change the subtraction to

```
       real delta = dsin;
       delta -= rsin;
```

getting

sinus_e1.d(12): Error: cannot implicitly convert expression dsin 
of type Decimal!128 to real

Changes this into

```
       real delta = dsin.to!real;
       delta -= rsin;
```
sinus_e2.d(12): Error: template decimal.to cannot deduce function 
from argument types !(real)(Decimal!128), candidates are:
decimal/package.d(5814):        decimal.to(T, D)(auto ref const D 
x, const RoundingMode mode) if (isIntegral!T && isDecimal!D)
decimal/package.d(5832):        decimal.to(F, D)(auto ref const D 
x, const RoundingMode mode) if (isFloatingPoint!F && isDecimal!D)


to no avail. Also

```
       real delta = cast(real) dsin;
```

did not succeed:

decimal/package.d(933): Error: undefined identifier mode, did you 
mean template modf(D)(auto ref const D x, ref D y) if 
(isDecimal!D)?
sinus_e3.d(12): Error: template instance 
decimal.Decimal!128.Decimal.opCast!real error instantiating

Also

```
       real delta = dsin.to!(real, RoundingMode.tiesToEven);
```

ain't work:
sinus_e4.d(12): Error: template decimal.to cannot deduce function 
from argument types !(real, cast(RoundingMode)0)(Decimal!128), 
candidates are:
decimal/package.d(5814):        decimal.to(T, D)(auto ref const D 
x, const RoundingMode mode) if (isIntegral!T && isDecimal!D)
decimal/package.d(5832):        decimal.to(F, D)(auto ref const D 
x, const RoundingMode mode) if (isFloatingPoint!F && isDecimal!D)

How can I convert a decimalX to float/double/real?


More information about the Digitalmars-d-announce mailing list