PDA

Click to See Complete Forum and Search --> : Double forced into an int


Buy2easy.com
Feb 26th, 2003, 05:44 PM
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.

Emo
Feb 26th, 2003, 06:18 PM
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

Jop
Feb 26th, 2003, 06:31 PM
if you're not sure about something like this, you could always test it by executing it :)


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)
}

CornedBee
Feb 27th, 2003, 06:02 AM
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.


double d = sqrt(16);
if(d ==4.0)
cout << "Exact" << endl;
else
cout << "Inexact" << endl;


This should print Exact, but it might not.

Buy2easy.com
Feb 27th, 2003, 09:30 AM
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.

jim mcnamara
Feb 27th, 2003, 10:06 AM
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

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);
}

CornedBee
Feb 27th, 2003, 10:58 AM
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.

Buy2easy.com
Feb 27th, 2003, 02:06 PM
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.