|
-
Feb 26th, 2003, 06:44 PM
#1
Thread Starter
Addicted Member
Double forced into an int
is this true?
int myInt;
double myDouble;
scanf("%lf", &myDouble);
myInt = myDouble
if(myInt == myDouble)
{
proceed............
would they equal if the input was say 123.00 but not equal if you input 123.01
i am pretty sure that my asumption is right but let me know if i am wrong.
-
Feb 26th, 2003, 07:18 PM
#2
Hyperactive Member
Re: Double forced into an int
Originally posted by Buy2easy.com
int myInt;
double myDouble;
scanf("%lf", &myDouble);
2 questions about that...
I'm not sure about this but is there such a thing as Only 'Double'? isn't it double int or something?
and why are you putting %lf? it's that used for long float?
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 26th, 2003, 07:31 PM
#3
Frenzied Member
if you're not sure about something like this, you could always test it by executing it 
Code:
void testIntDouble(){
int i = 123;
double f = 123.01;
cout << (i == f) << endl; //displays 0 (false)
f = 123.00;
cout << (i == f) << endl; //displays 1 (true)
}
Last edited by Jop; Feb 26th, 2003 at 07:34 PM.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 27th, 2003, 07:02 AM
#4
No, there is no double int. double is a type of its own. int means an integer, a whole number, not a real number as float or double.
Equality tests of floating point values (float or double) are always risky. If the double is the result of a calculation then it might be just a little inaccurate - enough that an equality test fails.
Code:
double d = sqrt(16);
if(d ==4.0)
cout << "Exact" << endl;
else
cout << "Inexact" << endl;
This should print Exact, but it might not.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 27th, 2003, 10:30 AM
#5
Thread Starter
Addicted Member
i was just woundering if a double type could be placed into a int type. like:
int = double
and if the double was 120.000 thenthe int would be 120 but 120.000 is equal to 120 but if the input was a 120.25 then the int would just be 120 because you would lose the .25 so then the double would be larger.
this was for a program that i had due for class so there are reasons that it had to be checked aginst each other.
-
Feb 27th, 2003, 11:06 AM
#6
Frenzied Member
Not directly. Double is floating point, int, long, short are purely binary numbers.
Type conversion is what this is called. In C++ you can sometimes overload operatiors and create implicit data conversions, but in C
you have to explicitly convert datatypes.
C example
Code:
int dbl_to_int(double z){
char tmp[14];
sprintf(tmp,"%.0f",z);
return atoi(tmp);
}
double int_to_dbl(int z){
char tmp[14];
sprintf(tmp,"%d",z);
return atof(tmp);
}
-
Feb 27th, 2003, 11:58 AM
#7
Huh? I don't know about your compiler, but ANSI C dictates that float and double get implicitly converted to int with rounding method set to floor, optionally issuing a warning about data loss.
Thus:
double d = 31.999;
int i = d;
will compile with i then having the value 31. It will probably issue a warning which can be muted by explicit casting:
int i = (int)d;
In both cases it should generate code that instructs the FPU to generate an integer from the floating point number.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 27th, 2003, 03:06 PM
#8
Thread Starter
Addicted Member
i was about to say. I just did it and it works fine. Just like i had anticipated. The reason i was woundering is because i had to write a program that wouldreject anything that was not a whole number and i thought that might be a good way to check. There is probably onouther way to do it but that one is the best one i could think of.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|