Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C How to calculate a percentage(perthousands) without floating point precision

Tags:

People also ask

How do you calculate percentage without percentage button?

2. If your calculator does not have a “%” button. Step 1: Remove the percent sign and add a couple of zeros after the decimal point.

How do you find the percentage point value?

Percentage points = Percentage #2 - Percentage #1 = 7 - 5 = 2 . Add the unit (percentage point) and the direction of the change (increase if positive, decrease if negative, or simply state as difference) to the number: There was a 2 percentage point increase in the unemployment rate from 2019 to 2020.

Can percentage float?

Explanation: When percentage values are in a table and they are in float form, they will likely be displayed as a decimal (Ex: 0.072 , 0.093).


How do you calculate a percentage from 2 int values into a int value that represents a percentage(perthousands for more accuracy)?

Background/purpose: using a processor that doesn't have a FPU, floating point computations take 100's of times longer.

int x = 25;
int y = 75;
int resultPercentage; // desire is 250 which would mean 25.0 percent

resultPercentage = (x/(x+y))*1000; // used 1000 instead of 100 for accuracy
printf("Result= ");
printf(resultPercentage);

output:

Result= 0

When really what I need is 250. and I can't use ANY Floating point computation.

Example of normal fpu computation:

int x = 25;
int y = 75;
int resultPercentage; // desire is 250 which would mean 25.0 percent

resultPercentage = (int)( ( ((double)x)/(double(x+y)) ) *1000); //Uses FPU slow

printf("Result= ");
printf(resultPercentage);

output:

Result= 250

But the output came at the cost of using floating point computations.