www.digitalmars.com         C & C++   DMDScript  

D - Is this is intentional (or a bug)? (1/3 equals 0)

reply SL <shadowlord13 users.sourceforge.net> writes:
Basically, I wrote this out:
radius=pow((volume*3)/(4*PI),1/3);

And radius was coming out to 1 every time. So I made a printf to show 
various parts of that.

printf("PI=%.*s volume=%.*s (volume*3)/(4*PI)=%.*s radius=%.*s\n", 
.toString(PI), .toString(volume), .toString((volume*3)/(4*PI)), 
.toString(radius));

PI=3.14159 volume=71.1094 (volume*3)/(4*PI)=16.9761 radius=1

And then I did this:
printf("1/3=%.*s",.toString(1/3));

And got:
1/3=0

I suppose this is intentional (Since both numbers are integers)?

P.S. I have changed it to 1/3.0, which works, but does not look like a 
proper fraction :P.
-SL
Mar 26 2004
next sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
It is a C-ism. Due to lack of proper beginners' books on D, it is very 
advantageous to learn some of C before going to D. Also, everything 
which works the same way as in C or C++ has been left out of the 
documentation.

-eye

SL schrieb:
 Basically, I wrote this out:
 radius=pow((volume*3)/(4*PI),1/3);
 
 And radius was coming out to 1 every time. So I made a printf to show 
 various parts of that.
 
 printf("PI=%.*s volume=%.*s (volume*3)/(4*PI)=%.*s radius=%.*s\n", 
 .toString(PI), .toString(volume), .toString((volume*3)/(4*PI)), 
 .toString(radius));
 
 PI=3.14159 volume=71.1094 (volume*3)/(4*PI)=16.9761 radius=1
 
 And then I did this:
 printf("1/3=%.*s",.toString(1/3));
 
 And got:
 1/3=0
 
 I suppose this is intentional (Since both numbers are integers)?
 
 P.S. I have changed it to 1/3.0, which works, but does not look like a 
 proper fraction :P.
 -SL
Mar 26 2004
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
SL wrote:

<snip>
 And got:
 1/3=0
 
 I suppose this is intentional (Since both numbers are integers)?
I think so.
 P.S. I have changed it to 1/3.0, which works, but does not look like a 
 proper fraction :P.
 -SL
Does cast(real) 1/3 look more like it to you? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Mar 26 2004