C math calculation does not work as expected

I have the following in the program (part of a much larger function, but this is the corresponding bit of testing):

int test = 100 + (100 * (9 / 100)); sprintf (buf, "Test: %d\n\r", test); display_to_pc (buf, player); 

This basically amounts to:

 x = a + (a * (b / 100)) 

If a is the given figure, b is the percentage modifier, and x is the result (the original plus the percentage of the original) ... I hope this makes sense.

This gives me:

 Test: 100 

I thought that the math in my head might be wrong, but I checked several calculators and even an expression evaluator in my IDE, and all of them give me the expected result 109 for the first expression.

Can someone enlighten me about what I do not see here?

Thanks a lot.:)

+4
source share
11 answers

Replace

 int test = 100 + (100 * (9 / 100)); 

with

 int test = 100 + (100 * 9 / 100); // x = a + (a * b / 100) 

and it will work as expected. 9 / 100 is performed using integer division; the closest integer to .09 is 0 (and 0 * 100 is still 0 ).

Doing the multiplication first leads to 900 / 100 , which gives you the 9 you expected.

If you need more than integer accuracy, you may need a floating point route.

+14
source

You are using integer math.

9/100 = 0.

100 + (100 * (0) = 100.

Your calculators use floating point math and can process decimals accordingly.

+8
source

As others have noted, the problem is that you are doing integer arithmetic. You need to do the calculation as a floating point by adding a variable to double or using a double constant, and then enable truncation to give you only the integer part.

 x = a + (a * (b / 100.0)); 

or

 x = a + (a * ((double)b / 100))); 
+4
source

int test = 100 + (100 * (9/100));

9/100 = 0

0 * 100 = 0

100 + 0 = 100

+3
source

Your mistake is that 9/100 is interpreted as integer division and evaluated as 0, not 0.09.

Instead, you can write 9 / 100.0 or change the expression.

+3
source

you use integer division, so 9/100 is zero, so test = 100 + 0 = 100

+2
source

Use Daniel L's answer if you only work with expressions that result in integer integer values. If the values ​​you'll work with are less clean, use double literals instead of whole literals:

 int test = 100.0 + (100.0 * (9.0 / 100.0)); 
+2
source

The answer of 9/10 is truncated to 0, and then multiplied by 100, and then adds 100 to it.

+1
source

Edit:

 int test = 100 + (100 * (9 / 100)); 

to

 int test = (int)(100 + (100 * (9 / 100.0))); 

and see if it works as expected.

EDIT: wow. A lot of answers while I was typing. Most of them will work too.

+1
source

You should use floating point arithmetic so that 9/100 becomes 0.09.

int test = 100.0 + (100.0 * 9.0 / 100.0);

+1
source

You use integers for your types of variables, so the majority of expressions (9/100) will result in 0. Then the next expression will be 100 * 0, which is 0, and finally 100 + 0 ...

To get the desired result, try using float instead:

float test = 100.0 + (100.0 * (9.0 / 100.0));

printf ("result:% 0.2f \ n", test);

0
source

All Articles