Thread: Silly double to int casting

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18

    Silly double to int casting

    *sigh* this has to be super simple, but I think my brain if getting fried from coding today.

    I have an int and a double...i know the double is actually an integer value (in this case, but I cannot change the double declaration to int). All I wanna do is subtract the int values of both vars, and its being silly:

    Code:
    printf("[Clauses] %d -  [obj best] %17.10e = [fitness] %d \n", sat_clauses, objective_best, sat_clauses-objective_best);
    printf("[Clauses] %d -  [obj best] %17.10e = [fitness] %d \n", sat_clauses, (int)objective_best, sat_clauses-(int)objective_best);
    Output:

    [Clauses] 298 - [obj best] 2.9700000000e+02 = [fitness] 0
    [Clauses] 298 - [obj best] 2.1219959377e-314 = [fitness] 0

    While the result i'm looking for is
    [Clauses] 298 - [obj best] 297 = [fitness] 1

    Don't yell, I know its something stupid

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Cast the double to an integer when you subtract those variables.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18
    I am doing that in the second print attempt:

    sat_clauses-(int)objective_best
    Last edited by otaconBot; 08-08-2012 at 09:36 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Note the scientific notation. The second "objective_best" is a very very tiny number.

    Code:
    1st "objective_best" = 2.9700000000e+02 = 297.00000...
    
    2nd "objective_best" = 2.1219959377e-314 = 0.00000...000212100  // <--- 313 zeroes between the decimal point and the first non-zero digit
    [edit]
    What is the value of the second "objective_best" if you don't cast the second argument to an "int"?
    Last edited by Matticus; 08-08-2012 at 09:43 AM.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18
    Yea, I noticed that, but the actual value of both "objective_best" is the same, and should be (in this case) 297.0. Not sure why when I cast it to int it suddenly becomes this tiny number in the second print

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Because you're not printing it as an int for the second argument, you're printing it as a float/double with %e. Try changing that to %d and see what value it prints.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18
    Wow, two things... you're right, after casting it to int I forgot to reformat the print!

    Code:
    printf("[Clauses] %d -  [obj best] %d = [fitness] %d \n", sat_clauses, (int)objective_best, sat_clauses-(int)objective_best);
    Output:
    [Clauses] 298 - [obj best] 297 = [fitness] 1

    Exactly what I needed! Thank you!

    Second thing....WHY DOES THAT WORK? I mean, yea, the second part of the format was wrong, but it should only affect the print of the second part, not the 3rd one! (i mean the "sat_clauses-(int)objective_best", which was formatted to %d correctly!)
    Last edited by otaconBot; 08-08-2012 at 09:55 AM.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by otaconBot View Post
    Second thing....WHY DOES THAT WORK? I mean, yea, the second part of the format was wrong, but it should only affect the print of the second part, not the 3rd one! (i mean the "sat_clauses-(int)objective_best", which was formatted to %d correctly!)
    The arguments are most likely pushed onto a stack, and printf pulls the right number of bytes off of that stack for each argument, depending on the type specified in the format string. There is no delineation between each argument on the stack, so printf doesn't know where each argument actually starts, nor does it know how big each argument is, except by the format string that you give it. So if you pass an int value where printf expects a double value, printf will interpret the bytes as a double and will skip the size of a double rather than the size of an int. Presumably on your system a double is not the same size as an int (most likely a double is 8 bytes and int is 4 bytes). So printf actually "eats" two int values when it expects a double.

    Note that this describes the behavior on a common implementation (eg, most 32-bit and 64-bit compilers on x86). The actual behavior according to the standard is either undefined or implementation defined (a standards guru can clarify this point).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting int to double in pow()
    By l0ul0u in forum C Programming
    Replies: 2
    Last Post: 11-14-2011, 05:56 PM
  2. trouble casting char to double
    By deprekate in forum C Programming
    Replies: 16
    Last Post: 03-03-2009, 03:18 PM
  3. casting double to singed int
    By patiobarbecue in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2009, 04:24 PM
  4. Casting unsigned long division to a double?
    By smoothdogg00 in forum C Programming
    Replies: 5
    Last Post: 12-22-2006, 09:22 AM
  5. Rounding error when casting double to short
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 10-25-2006, 12:48 PM