Results 1 to 8 of 8

Thread: Double forced into an int

  1. #1

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    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.

  2. #2
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428

    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²™¤»

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    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.

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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);
    }

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    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
  •  



Click Here to Expand Forum to Full Width