# 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. ## 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. 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.

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.

3. I thought 13/4 equals 3.25?

4. and how come when i do d = (13 / 4); d only equals 3 even before rounding d.

5. 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.

6. 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.

7. Thanks Salem. So what the hell does 13 % 4 do?

8. 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. Originally Posted by errigour
So what the hell does 13 % 4 do?
It results in the remainder of 13 divided by 4.

10. so 13.0 % 4 = .25? if yes thanx if not i'm lost. Thanx!

11. 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;
}```

12. Originally Posted by errigour
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. 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. Cast one of the parts to a double first:
Code:
`b = (double) i / 4;`

15. What happen's to integers when the sum is never ending?

Page 1 of 2 12 Last