double integer math problems surpass my programming skills

This is a discussion on double integer math problems surpass my programming skills within the C Programming forums, part of the General Programming Boards category; ok heres the code: Code: #include <stdio.h> #include <math.h> int main (int argc, char *argv[]) { double d; int j; ...

  1. #1
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    double integer math problems surpass my programming skills

    ok heres the code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (int argc, char *argv[])
    {
    	double d; int j;
    
    	d = (13 % 4);
    	printf("rint : d = %f\r\n", d);
    	j = rint(d);
    	printf("rint : d = %f, j = %d\r\n", d, j);
    	d = (13 % 4);
    	printf("round: d = %f\r\n", d);
    	j = round(d);
    	printf("round: d = %f, j = %d\r\n", d, j);
    }
    heres the output:
    Code:
    rint : d = 1.000000
    rint : d = 1.000000, j = 1
    round: d = 1.000000
    round: d = 1.000000, j = 1
    How come d doesn't equal 3.25 and then how come d doesn't equal 3 or four depending on the round function?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by errigour
    How come d doesn't equal 3.25
    Why should d be 3.25? You assign (13 % 4) to it, and 13 % 4 == 1.

    Quote Originally Posted by errigour
    d doesn't equal 3 or four depending on the round function?
    You're not assigning the result of round(d) to d anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    I thought 13/4 equals 3.25?

  4. #4
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    and how come when i do d = (13 / 4); d only equals 3 even before rounding d.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    13 / 4 is done in integer maths, THEN the result is converted to a double and then assigned to d.

    13.0 / 4.0 is done using floating point maths, and then assigned to d.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by errigour
    I thought 13/4 equals 3.25?
    13/4 and 13%4 are very different expressions. Look at which one you actually used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Thanks Salem. So what the hell does 13 % 4 do?

  8. #8
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    I have never understood the % sign does maybe you could explain in to me in a way that I could understand, or not at all.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by errigour
    So what the hell does 13 % 4 do?
    It results in the remainder of 13 divided by 4.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    so 13.0 % 4 = .25? if yes thanx if not i'm lost. Thanx!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by errigour
    13.0 % 4 = .25?
    No, because % is not defined for floating point numbers. Rather, #include <math.h> and use fmod, e.g.,
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        printf("%f\n", fmod(13.0, 4));
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    Quote Originally Posted by errigour View Post
    so 13.0 % 4 = .25? if yes thanx if not i'm lost. Thanx!
    Code:
    13 % 4 == 1
    
    Modulus (%) gives the remainder of division:
    
    ------------
      ____
    4|13
    
    ------------
       3
      ____
    4|13
    
    ------------
       3
      ____
    4|13
      12
      ----
       1  <-- remainder
    ------------

  13. #13
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    hey is there any way to get the value of a division of integers into a double etc...
    int i, j;
    double b;
    i = 14;
    j = 4;
    b = i / 4;

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Cast one of the parts to a double first:
    Code:
    b = (double) i / 4;

  15. #15
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    What happen's to integers when the sum is never ending?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to programming - Lack math skills
    By EdMarx in forum C Programming
    Replies: 16
    Last Post: 05-23-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-07-2011, 10:05 PM
  3. help my math (int/double)
    By major_small in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2003, 01:36 PM
  4. Very large signed integer math question
    By Criz in forum C Programming
    Replies: 8
    Last Post: 12-01-2002, 11:43 AM
  5. Need math skills
    By Megatron in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21