I am trying to calculate a long value divided by an integer to give me what I would expect to be a double, although the result I am getting is 0. The code I am using...
double daysByYear = daysSinceBirthdayToService/365;
NSLog(#"%d", daysByYear);
In this code, daysSinceBirthdayToService variable is a Long Double which can be NSLogged using the following code (long)daysSinceBirthdayToService
It is declaired in the header file as a property of
#property (nonatomic) NSInteger daysSinceBirthdayToService;
Can anybody help me out with this, thanks!
The issue is that / between two longs will do an integral division.
To force a floating point division at least one of the operands needs to be cast to double.
e.g.
double daysByYear = daysSinceBirthdayToService/(double)365;
or if you have a literal make that a double by adding a decimal point
double daysByYear = daysSinceBirthdayToService/365.0;
double daysByYear = daysSinceBirthdayToService/365.0;
Can it be that %d outputs decimal number not a double?
Related
In my program, there are some decimal values that should be defined float respect to their range.
But, in several calculations (multiply), ranges might be larger than 10^38, so I need to convert them to Double before the calculation.
Say the values are
let a: Float // maximum: 10
let b: Float // maximum: 10^20
let c: Float // maximum: 10^30
and the calculations are like
func multiplyAB() -> Float {
return a * b
}
func multiplyBC() -> Double {
return Double(b) * Double(c)
}
let d = multiplyBC()
What bothers me is which one is better performance-wise?
Convert from Float to Double during calculation or define a, b, c as Double?
In other words, is converting from Float to Double a handy job to CPU (like realloc memory, handle precision and sort of things) comparing to calculate all numbers in Double?
BTW, why Apple use Double as the underlying value for CGFloat?
Maximum value for Float is 10^38, which is pretty large respect to iPhone screen sizes and pixels can't be float (10.111 and 10.11 make no difference, right?).
What's the reason behind that?
from
THE SWIFT PROGRAMMING LANGUAGE
"NOTE
Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, Double is preferred."
I wrote a sample to convert string to float in Objective-C:
NSString *sampleFloatString = #"1.3";
float sampleFloatValue = [sampleFloatString floatValue];
But when I display sampleFloatValue, it shows '1.29999995'. I know it's equal to 1.3, but why is it not exactly '1.3'? Why do we need to format it explicitly? Is there any other way of doing this conversion?
Its called "Floating point error". The way that computers represent decimal numbers causes them to not be 100% accurate all the time:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Try this
float sampleFloatValue = (float) sampleFloatString;
Hope it helps.
if you just wants to show these value somewhere than u can do these and it shows 1.3 exact..
NSString *sampleFloatString = #"1.3";
float sampleFloatValue = [sampleFloatString floatValue];
NSLog(#"%.1f",sampleFloatValue);
in iOS Objetive-C I am trying to get the number typed by the user in a text field to set the upper bounder of a random number generation function in C.
- (IBAction)pushTheButton2:(id)sender {
u_int32_t upperBound = (u_int32_t) textField3.text;
textField4.text = [NSString stringWithFormat:#"%d", arc4random_uniform(upperBound)];
}
The output is a giant number that makes no sense. To test if function works, if I hardcode the actual upper bound in the arc4random_uniform function, such as arc4random_uniform(5), then it works!
I figured this could be some kind of literal conversion, so I tried to make this work with u_int32_t but still not outputting the right range.
Can someone help? Thanks
You are currently taking the memory reference pointer of the text and using that as the upper bound.
Try doing something like this instead...
NSInteger upperBound = [textfield.text intValue];
This will convert the string into an int that you can then use in the arc random function.
To parse string to integer you should do:
NSInteger upperBound = [textfield.text integerValue];
I have a problem with my float variable, i have to do some operation and then i have a final number what I saw is that some time the number is not correct, but just for a point for example when i have this number and i try to print it i don't give me back the same number:
float myNumber = 27589353.0f;
NSLog(#"My Number is %.2f", myNumber);
the result is: My Number is 27589352.00
I've tried to put the variable double but i have the same issue.
The problem is that floats do not have enough precision, as maddy said in the comments above.
And this code:
double myNumber = 27589353.0f;
NSLog(#"My Number is %.2f", myNumber);
Won't work either because the "f" qualifier on the constant forces the number to be a float, causes the loss of precision, then promotes the value to a double, once the damage is done.
This code however, will work correctly:
double myNumber = 27589353.0;
NSLog(#"My Number is %.2f", myNumber);
(Note that I'm assigning a floating point value with a decimal, but no final "f"
try like that it's work, you have to remove the 'f' as well
double myNumber = 27589353.0;
NSLog(#"My Number is %.2f", myNumber);
I have app with three view controllers. First is UIViewController with three UITextField's where user put some digits. These digits are stored to my CoreData entity as String attributes.
Second is UITableView, where I show my stored data from CoreData as new cell.
Third is detail UIViewController where I show user all his previously inserted digits.
The problem is when I set on textField decimal pad, user have digits and comma but my function need digits with dot for double precision calculating.
With comma I can't make mathematical function as [.....] * [...] = .... because it doesn't work.
Any idea how I can figure it out?
Can I simply change that comma to a dot?
I have this code:
NSNumberFormatter*nf = [[NSNumberFormatter alloc]init];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
double result = [myString doubleValue] * [myOtherString doubleValue];
You want the decimal keypad to have either a comma or a period (dot) based on the user's locale. This is so the user can enter the value as they are accustomed to. In order for you to do the math with the vales, you need to use a NSNumberFormatter to convert the entered string into a double value. Once you do this, your math formulas will work. Never use doubleValue or floatValue to convert an NSString to a double or float if the string was entered by the user. Always use NSNumberFormatter to properly deal with the user's locale.
Update based on code added to question.
You don't use the number formatter. You are doing exactly what I said not to do. Change your code to:
double firstValue = [[nf numberFromString:myString] doubleValue];
double secondValue = [[nf numberFromString:myOtherString] doubleValue];
double result = firstValue * secondValue;
Solution is very simple:
1. Go to Settings > General > International > Regional Format
2. Now select "United States"
Now run your App and see dot(.) instead of comma(,) in Decimal pad keyboard.