Thread overview
Casting to an imaginary float, zeros out the casted value
Oct 26, 2005
David L. Davis
Oct 26, 2005
Walter Bright
Oct 26, 2005
David L. Davis
Oct 26, 2005
Walter Bright
October 26, 2005
# // ift.d dmd v0.137 WinXP SP2
# // Casting to an imaginary float, zeros out the casted value
# private import std.stdio;
#
# int main()
# {
#     creal  cr   = 234.56e-12+789.675i;
#     ifloat ift1 = cast(ifloat)cr.im; // should be 789.675i, not 0.0i
#     ifloat ift2 = 789.675i;
#
#     writefln("cr=%g", cr, " cr.im=%g", cr.im,
#              " ift1=%g", ift1, " ift2=%g", ift2);
#
#     writefln("f=", cast(float)cr.im, " d=", cast(double)cr.im,
#              " r=", cast(real)cr.im); // no problem here
#
#     writefln("id=", cast(idouble)cr.im, " ir=", cast(ireal)cr.im);
#     return 0;
# }

Output:
----------------
C:\dmd>dmd ift.d
C:\dmd\bin\..\..\dm\bin\link.exe ift,,,user32+kernel32/noi;

C:\dmd>ift
cr=2.3456e-10+789.675i cr.im=789.675 ift1=0 ift2=789.675
f=789.675 d=789.675 r=789.675
id=0 ir=0

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
October 26, 2005
"David L. Davis" <SpottedTiger@yahoo.com> wrote in message news:djmijl$1mjj$1@digitaldaemon.com...
> # // Casting to an imaginary float, zeros out the casted value

That's the way it's supposed to work, as a real number has a 0 imaginary part. If you want to take a real number and make an imaginary number out of it, multiply it by 1i.


October 26, 2005
In article <djn4vl$3ia$3@digitaldaemon.com>, Walter Bright says...
>
>
>"David L. Davis" <SpottedTiger@yahoo.com> wrote in message news:djmijl$1mjj$1@digitaldaemon.com...
>> # // Casting to an imaginary float, zeros out the casted value
>
>That's the way it's supposed to work, as a real number has a 0 imaginary part. If you want to take a real number and make an imaginary number out of it, multiply it by 1i.
>

Opps!! My mistake Walter...somehow I forgot that the .im of a complex float is only the value of the imaginary number piece and not itself an imaginary number value. Yet it seems so natural that it should be an imaginary number value, plus it seems a bit odd the multiply .im by 1.0i (when im = imaginary number) to make it an imaginary number. If you know what I mean?

This approach works just fine:
creal cr = 234.56e-12+789.675i;
real   r = cast(real)cr;  // r  = 2.3456e-10
ireal ir = cast(ireal)cr; // ir = 789.675

So why not this, without the need to cast?
real  r  = cr.re; // this works
ireal ir = cr.im; // this won't without a cast(ireal)cr.im * 1.0i

Thanks again for your reply.

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
October 26, 2005
"David L. Davis" <SpottedTiger@yahoo.com> wrote in message news:djoc35$2m8b$1@digitaldaemon.com...
> Opps!! My mistake Walter...somehow I forgot that the .im of a complex
float is
> only the value of the imaginary number piece and not itself an imaginary
number
> value. Yet it seems so natural that it should be an imaginary number
value, plus
> it seems a bit odd the multiply .im by 1.0i (when im = imaginary number)
to make
> it an imaginary number. If you know what I mean?
>
> This approach works just fine:
> creal cr = 234.56e-12+789.675i;
> real   r = cast(real)cr;  // r  = 2.3456e-10
> ireal ir = cast(ireal)cr; // ir = 789.675
>
> So why not this, without the need to cast?
> real  r  = cr.re; // this works
> ireal ir = cr.im; // this won't without a cast(ireal)cr.im * 1.0i

Because cr.im gets the imaginary part *as a real number*, from whence it follows the rules for real numbers.