View mode: basic / threaded / horizontal-split · Log in · Help
July 22, 2005
Help: Types Conversion libraries, where are thou?
Greetings!

Yes, it's me, your favorite Dominican learning D. :-)

Ok, so I need to change reals to integers and integers to real, int to long,
etc.  I have search through digitalmars.com and found nothing about this.  I did
see the phobos std.boxer, which is not working for me.  For example, this
program,

import std.stdio;
import std.math;
import std.string;
import std.boxer;
real toReal(int i)
{
Box b = box(i);
real r = unbox!(real)(b);
return r;
}
int toInt(real r)
{
Box b = box(r);
int i = unbox!(int)(b);
return i;
}
void main()
{
int i = 45; 
real r = 12.45;
writefln(i," ",r);
writefln(toReal(i)," ",toInt(r));
}

does not compile.  Here is the error:

jic 19:08:52-> build totypes.d 
c:\dmd\bin\..\..\dm\bin\link.exe
totypes,totypes.exe,,user32+kernel32,totypes.def/noi;
OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

totypes.obj(totypes) 
Error 42: Symbol Undefined _assert_3std5boxer
--- errorlevel 1

What are the libraries that I have to do this or do I need to write my own?

thanks,

josé
July 23, 2005
Re: Help: Types Conversion libraries, where are thou?
Well, how about simply using the cast() operator?

Cheers,
Holger


In article <dbrung$1t68$1@digitaldaemon.com>, jicman says...
>
>
>Greetings!
>
>Yes, it's me, your favorite Dominican learning D. :-)
>
>Ok, so I need to change reals to integers and integers to real, int to long,
>etc.  I have search through digitalmars.com and found nothing about this.  I did
>see the phobos std.boxer, which is not working for me.  For example, this
>program,
>
>import std.stdio;
>import std.math;
>import std.string;
>import std.boxer;
>real toReal(int i)
>{
>Box b = box(i);
>real r = unbox!(real)(b);
>return r;
>}
>int toInt(real r)
>{
>Box b = box(r);
>int i = unbox!(int)(b);
>return i;
>}
>void main()
>{
>int i = 45; 
>real r = 12.45;
>writefln(i," ",r);
>writefln(toReal(i)," ",toInt(r));
>}
>
>does not compile.  Here is the error:
>
>jic 19:08:52-> build totypes.d 
>c:\dmd\bin\..\..\dm\bin\link.exe
>totypes,totypes.exe,,user32+kernel32,totypes.def/noi;
>OPTLINK (R) for Win32  Release 7.50B1
>Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved
>
>totypes.obj(totypes) 
>Error 42: Symbol Undefined _assert_3std5boxer
>--- errorlevel 1
>
>What are the libraries that I have to do this or do I need to write my own?
>
>thanks,
>
>josé
>
>
July 23, 2005
Re: Help: Types Conversion libraries, where are thou?
In article <dbrung$1t68$1@digitaldaemon.com>, jicman says...
>
>
>Greetings!
>
>Yes, it's me, your favorite Dominican learning D. :-)
>
>Ok, so I need to change reals to integers and integers to real, int to long,
>etc.  I have search through digitalmars.com and found nothing about this.  I did
>see the phobos std.boxer, which is not working for me.  For example, this
>program,
>
>import std.stdio;
>import std.math;
>import std.string;
>import std.boxer;
>real toReal(int i)
>{
>    Box b = box(i);
>    real r = unbox!(real)(b);
>    return r;
>}
>int toInt(real r)
>{
>    Box b = box(r);
>    int i = unbox!(int)(b);
>    return i;
>}
>void main()
>{
>    int i = 45; 
>    real r = 12.45;
>    writefln(i," ",r);
>    writefln(toReal(i)," ",toInt(r));
>}
>
>does not compile.  Here is the error:
>
>jic 19:08:52-> build totypes.d 
>c:\dmd\bin\..\..\dm\bin\link.exe
>totypes,totypes.exe,,user32+kernel32,totypes.def/noi;
>OPTLINK (R) for Win32  Release 7.50B1
>Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved
>
>totypes.obj(totypes) 
>Error 42: Symbol Undefined _assert_3std5boxer
>--- errorlevel 1
>
>What are the libraries that I have to do this or do I need to write my own?
>
>thanks,
>
>josé
>
>

josé, to compile using the std.boxer you should always use the "-release"
commandline switch...but in your conversion example above doesn't appear to need
the std.boxer for your 'int to real' / 'real to int' example.

So, I put a little something together below that I hope you'll find helpful:

# // ex072205.d
# private import std.stdio;
# 
# int roundToInteger(in real r) 
# { 
#     return cast(int)((r < 0.0L) ? r - 0.5L : r + 0.5L); 
# }
# 
# real toReal(int i)
# {
#     return cast(real)i;
# }
# 
# int toInt(real r)
# {
#     if (r < int.min || r > int.max)
#         diehard("Real value is larger or smaller than an integer.");
#     else if (r == real.nan)
#         diehard("Can't convert a NaN to an integer.");
#     else if (r == -real.infinity || r == real.infinity)
#         diehard("Can't convert an +/- Infinity value to an integer.");
# 
#     return roundToInteger(r);
# }
# 
# private void diehard(in char[] s)
# {
#     throw new Exception(s);
# }
# 
# void main()
# {
#     int  i = 45; 
#     real r = 12.45;
# 
#     writefln("Normal: int=", i, " real=", r);
#     writefln("Conversion: int=", toReal(i), " real=", toInt(r));
# }

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

C:\dmd>ex072205
Normal: int=45 real=12.45
Conversion: int=45 real=12

C:\dmd>

David L.

P.S. If you're still looking into some useful functions to use with the
std.boxer module you can check out my Box-Xtras Support section (link below).

http://spottedtiger.tripod.com/D_Language/D_BoxXtras_Support_Projects_XP.html

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

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
July 23, 2005
Re: Help: Types Conversion libraries, where are thou?
Thanks David.

josé

David L. Davis says...
>
>In article <dbrung$1t68$1@digitaldaemon.com>, jicman says...
>>
>>
>>Greetings!
>>
>>Yes, it's me, your favorite Dominican learning D. :-)
>>
>>Ok, so I need to change reals to integers and integers to real, int to long,
>>etc.  I have search through digitalmars.com and found nothing about this.  I did
>>see the phobos std.boxer, which is not working for me.  For example, this
>>program,
>>
>>import std.stdio;
>>import std.math;
>>import std.string;
>>import std.boxer;
>>real toReal(int i)
>>{
>>    Box b = box(i);
>>    real r = unbox!(real)(b);
>>    return r;
>>}
>>int toInt(real r)
>>{
>>    Box b = box(r);
>>    int i = unbox!(int)(b);
>>    return i;
>>}
>>void main()
>>{
>>    int i = 45; 
>>    real r = 12.45;
>>    writefln(i," ",r);
>>    writefln(toReal(i)," ",toInt(r));
>>}
>>
>>does not compile.  Here is the error:
>>
>>jic 19:08:52-> build totypes.d 
>>c:\dmd\bin\..\..\dm\bin\link.exe
>>totypes,totypes.exe,,user32+kernel32,totypes.def/noi;
>>OPTLINK (R) for Win32  Release 7.50B1
>>Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved
>>
>>totypes.obj(totypes) 
>>Error 42: Symbol Undefined _assert_3std5boxer
>>--- errorlevel 1
>>
>>What are the libraries that I have to do this or do I need to write my own?
>>
>>thanks,
>>
>>josé
>>
>>
>
>josé, to compile using the std.boxer you should always use the "-release"
>commandline switch...but in your conversion example above doesn't appear to need
>the std.boxer for your 'int to real' / 'real to int' example.
>
>So, I put a little something together below that I hope you'll find helpful:
>
># // ex072205.d
># private import std.stdio;
># 
># int roundToInteger(in real r) 
># { 
>#     return cast(int)((r < 0.0L) ? r - 0.5L : r + 0.5L); 
># }
># 
># real toReal(int i)
># {
>#     return cast(real)i;
># }
># 
># int toInt(real r)
># {
>#     if (r < int.min || r > int.max)
>#         diehard("Real value is larger or smaller than an integer.");
>#     else if (r == real.nan)
>#         diehard("Can't convert a NaN to an integer.");
>#     else if (r == -real.infinity || r == real.infinity)
>#         diehard("Can't convert an +/- Infinity value to an integer.");
># 
>#     return roundToInteger(r);
># }
># 
># private void diehard(in char[] s)
># {
>#     throw new Exception(s);
># }
># 
># void main()
># {
>#     int  i = 45; 
>#     real r = 12.45;
># 
>#     writefln("Normal: int=", i, " real=", r);
>#     writefln("Conversion: int=", toReal(i), " real=", toInt(r));
># }
>
>Output:
>---------
>C:\dmd>dmd ex072205.d
>C:\dmd\bin\..\..\dm\bin\link.exe ex072205,,,user32+kernel32/noi;
>
>C:\dmd>ex072205
>Normal: int=45 real=12.45
>Conversion: int=45 real=12
>
>C:\dmd>
>
>David L.
>
>P.S. If you're still looking into some useful functions to use with the
>std.boxer module you can check out my Box-Xtras Support section (link below).
>
>http://spottedtiger.tripod.com/D_Language/D_BoxXtras_Support_Projects_XP.html
>
>-------------------------------------------------------------------
>"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
>-------------------------------------------------------------------
>
>MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Top | Discussion index | About this forum | D home