Objective-C: floorf( ) returns wrong value - objective-c

Here is the code:
float passedPrice = 2.953;
float placed = 1000.0; //3 decimals
NSLog("%f", placed); // Gives 2953;
float withNoFractions = floorf(passedPrice * placed);
The value stored in withNoFractions is 2952! It shall be 2953. What is really strange is that it works some time.

Many decimal floating point fractions cannot be represented as exact fractions in binary, so they have to be approximated. 2.953 is being approximated as something like 2.95299999. When you multiply by 1000, the result is 2952.99999, and when you get the floor of this, it's 2952.
To solve this, you can either use round() instead of ffloorf(), or you can add 0.5 before calling ffloorf():
float withNoFractions = floorf(passedPrice * placed + 0.5);

Related

Will dividing an NSUInteger by 2 result in a whole number?

I am trying to do this in Objective-C:
self.nsarray.count/2
If the count is equal to 5, will the result be 5/2 = 2.5 or 5/2 = 2?
I am NSLogging the answer and it only shows me 2. I'm not sure if that's the actual answer or if's 2, because I am forced to use the %u format to log the answer. Please also explain the 'why' of this result.
The division with two whole numbers in Objective-C always produces a whole number as a result, in your case it would be NSUInteger, and 2 is a valid result in this case. To get a result with floating point at least one of your operands should be float typed, or at least one of them should be casted to float, so here's some options:
// Second part of division is float, so result is float as well
float result = self.array.count/2.
// First part of division is float, so result is float as well
float result2 = (float)self.array.count/2 // or you can type ((float)self.array.count)/2 for more clearance
Note that casting result to float isn't valid on your case, for instance in (float) (5/2) the result would be a whole number of type float (2.0) as you only cast a NSIntger to float
Floats are usually formatted in NSLog format as %f or %g

How to round up digit?

I'm making calculation for my account that only take precise value like 1.256 > 1.26.
I want to round up digit, i search many things like roundf,floorf... but that works like :
IF float myFloat = 1.56 THEN myFloat = 2.0.
But i want my calculation like :
float myFloat = 1.235 then myFloat = 1.240.
That it's not effecting my main digit.It effect only on second place after dot.
so it not effecting digit before dot(.).
How do i achieve this ?
You need to multiple and divide by 100:
float myFloat = 1.235;
float roundedFloat = roundf(myFloat * 100.0) / 100.0;
roundedFloat will be equal to 1.24.

Rounding issue when using NSString and %f [duplicate]

I have a floating point number that have more decimal digits, for example:
float fRes = 10.0 / 3.0;
actually the fRes value is 3.3333333333333
it's possible set for example 2 decimal digits:
float fRes = 10.0 / 3.0;
// fRes is 3.333333333333333333333333
float fResOk = FuncRound( fRes, 2 );
// fResOk is 3.33
thanks in advance
I don't know where you are using this rounded number, but you should only round your value when displaying it to the user, there are C based format string ways to round floating point numbers for example
[NSString stringWithFormat:#"%.2f", value];
as you may have already read, floating point number are approximations of real numbers, so doing fResOk = roundf( fRes*100.0)/100.0; may not give you 3.33 but a number which is just as close as you can get with floating point number to 3.33.
Assuming that you're looking for the correct function to round to a certain number of digits, you'll probably find it easiest to do the following:
fResOk = roundf( fRes*100.0)/100.0;
That will multiply the value by 100 (giving you your 2 digits of significance), round the value, and then reduce it back to the magnitude you originally started with.

Returning a number less than 1

I am working on an app that needs to utilize a ratio of a given number and multiply that ratio times another number. Problem is that I can't get numbers less that 1 to give me the proper decimal ratio, instead it gives me zero (when it should be .5).
Example:
float number = 1/2; // This gives me zero
double number = 1/2; // This also gives me zero
If you don't specify decimal places you're using integers which means the calculation is performed with integer precision before the result is cast to the type on the LHS. You want to do the the following when using hard coded numbers in your code:
float number = 1.0f / 2.0f;
double number = 1.0 / 2.0;
If you're aiming to use integer variables for an operation, you'll want to cast them to the type that you want for your result.
Try this
float number = 1.0/2.0;
Remember that 1 is an int, so you are essentially taking
(int)1 / (int)2
which returns
(int)0
To cast variables that are ints, do
float number = (float)numerator / (float)denominator;

iOS round a float

I have a floating point number that have more decimal digits, for example:
float fRes = 10.0 / 3.0;
actually the fRes value is 3.3333333333333
it's possible set for example 2 decimal digits:
float fRes = 10.0 / 3.0;
// fRes is 3.333333333333333333333333
float fResOk = FuncRound( fRes, 2 );
// fResOk is 3.33
thanks in advance
I don't know where you are using this rounded number, but you should only round your value when displaying it to the user, there are C based format string ways to round floating point numbers for example
[NSString stringWithFormat:#"%.2f", value];
as you may have already read, floating point number are approximations of real numbers, so doing fResOk = roundf( fRes*100.0)/100.0; may not give you 3.33 but a number which is just as close as you can get with floating point number to 3.33.
Assuming that you're looking for the correct function to round to a certain number of digits, you'll probably find it easiest to do the following:
fResOk = roundf( fRes*100.0)/100.0;
That will multiply the value by 100 (giving you your 2 digits of significance), round the value, and then reduce it back to the magnitude you originally started with.