Thread overview
Compiler says cast then when done says no can do
May 21, 2008
Ty Tower
May 21, 2008
Mike James
May 21, 2008
Koroskin Denis
May 21, 2008
Tower Ty
May 21, 2008
I have this line

double balance=values[6]-values[5]; (values 5&6 are char[])

so I change to double balance=-(double)values[6]-(double)values[5]; compiler says : C style cast illegal, use cast(double)values[6]

so I change to double balance=cast(double)values[6]-cast(double)values[5]; compiler says: Error: e2ir: cannot cast from char[] to double

So as I see it the compiler message in this case at least is wrong or there is a problem casting from a char array to a double . Is it a problem with the compiler message?


May 21, 2008
Ty Tower Wrote:

> I have this line
> 
> double balance=values[6]-values[5]; (values 5&6 are char[])
> 
> so I change to double balance=-(double)values[6]-(double)values[5]; compiler says : C style cast illegal, use cast(double)values[6]
> 
> so I change to double balance=cast(double)values[6]-cast(double)values[5]; compiler says: Error: e2ir: cannot cast from char[] to double
> 
> So as I see it the compiler message in this case at least is wrong or there is a problem casting from a char array to a double . Is it a problem with the compiler message?
> 
> 

I've just tried it - no problems. My compiler version is 1.29.

Maybe try cast(double)(values[5]).

May 21, 2008
On Wed, 21 May 2008 15:25:48 +0400, Ty Tower <towerty@msn.com.au> wrote:

> I have this line
>
> double balance=values[6]-values[5]; (values 5&6 are char[])
>
> so I change to
> double balance=-(double)values[6]-(double)values[5];
> compiler says : C style cast illegal, use cast(double)values[6]
>
> so I change to
> double balance=cast(double)values[6]-cast(double)values[5];
> compiler says: Error: e2ir: cannot cast from char[] to double
>
> So as I see it the compiler message in this case at least is wrong or there is a problem casting from a char array to a double . Is it a problem with the compiler message?
>
>

What do you want to get by casting from char[] to double?

if `values[5]` stores something like "3.14" then cast won't return what you expect,
consider using something like double t = to!(double)("3.14"); or toDouble("2.73")

Both are defined in std.conv
May 21, 2008
Koroskin Denis Wrote:
> On Wed, 21 May 2008 15:25:48 +0400, Ty Tower <towerty@msn.com.au> wrote:
> > I have this line
> >
> > double balance=values[6]-values[5]; (values 5&6 are char[])
> >
> > so I change to
> > double balance=-(double)values[6]-(double)values[5];
> > compiler says : C style cast illegal, use cast(double)values[6]
> >
> > so I change to
> > double balance=cast(double)values[6]-cast(double)values[5];
> > compiler says: Error: e2ir: cannot cast from char[] to double
> >
> > So as I see it the compiler message in this case at least is wrong or there is a problem casting from a char array to a double . Is it a problem with the compiler message?
> >
> >
> 
> What do you want to get by casting from char[] to double?
> 
> if `values[5]` stores something like "3.14" then cast won't return what
> you expect,
> consider using something like double t = to!(double)("3.14"); or
> toDouble("2.73")
> 
> Both are defined in std.conv
dmd 1.024
I use Tango to!(double) compiles OK but toDouble does not
The char{} values has a changeable string like "300.00" in 5 6 & 7

I tried
    double olddebit   = Float.parse(values[5]);
    double oldcredit  = Float.parse(values[6]);
    double oldbalance = Float.parse(values[7]);
 I change the amounts here then
     double balance = oldbalance + olddebit - oldcredit - to!(double)(values[5]) +
                 to!(double)(values[6]);
     values[7]= to!(char[])(balance);
which compiles but the result is nan (not a number I assume)

    double olddebit   = to!(double)(values[5]);
    double oldcredit  = to!(double)(values[6]);
    double oldbalance = to!(double)(values[7]);

     double balance = oldbalance + olddebit - oldcredit - to!(double)(values[5]) +
                     to!(double)(values[6]);
      values[7]= to!(char[])(balance);

this also compiles but gives nan ?
May 27, 2008
"Ty Tower" wrote
>I have this line
>
> double balance=values[6]-values[5]; (values 5&6 are char[])
>
> so I change to
> double balance=-(double)values[6]-(double)values[5];
> compiler says : C style cast illegal, use cast(double)values[6]
>
> so I change to
> double balance=cast(double)values[6]-cast(double)values[5];
> compiler says: Error: e2ir: cannot cast from char[] to double
>
> So as I see it the compiler message in this case at least is wrong or there is a problem casting from a char array to a double . Is it a problem with the compiler message?

The error might be in the order of operations.  I think the compiler is casting your variable, then applying the index operation.  So if you add the appropriate parentheses for default order of precedence, it looks like:

(cast(double)values)[6] ...

So what you really want is:

cast(double)(values[6]) - cast(double)(values[5]);

or what might be simpler:

cast(double)(values[6] - values[5]);

to force the order of operations.

I have no idea what the proper order of operations is, so this all might be BS, but it's almost impossible to determine the correct order for normal people (i.e. people who don't write/read grammar expressions regularly). However, the order of operations is sort of built into the grammar on http://www.digitalmars.com/d/2.0/expression.html

What makes sense to me is the index operator should be applied before the cast, but apparently that's not the case here...

-Steve