Objective-C , Avoid negative float value to be displayed - ios

I have the following code :
float temp;
temp = 2.0f;
- (IBAction)MinusTouched:(id)sender {
if (temp>0.0) {
temp=temp-0.1f;
[TempValue setText:[NSString stringWithFormat:#"%.1f",temp]];
}
}
When i perform action by clicking on the button , temp value decreases. But it prints -0.0 after 0.1 , how can i trim the negative sign ?
I mean i want an absolute float value.

For making float negative values in positive use fabs
[TempValue setText:[NSString stringWithFormat:#"%.1f",fabs(temp)]];

Try to use this method
fabs(temp)

Related

Converting String to Float is not giving exact value in iOS

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);

Remove last digit when it's a 0 in double (or any other option) when comparing

I'm adding a bunch of coordinates into quad tree and when I'm asking for the closest coordinate near my location, sometimes I've coordinate with 0 at the end, added automatically perhaps by the quad tree or I don't know how.
The problem is when I'm asking the double value in my core data using predicate it won't match because of the 0 digit addition to the number.
I thought about removing it when I've 0 but I'm sure there is a better way doing it.
For example:
Near location 31.123456, 34.123456, the nearest is 31.123444, 34.123450
when '34.123450' is actually 34.12345 in the database.
//Convert float to String
NSString *str_lat = #"34.123450";
NSString *trimmedString=[str_lat substringFromIndex:MAX((int)[str_lat length]-1, 0)];
if([trimmedString isEqualToString:#"0"])
{
str_lat = [str_lat substringToIndex:[str_lat length] - 1];
}
else
{
}
NSLog(#"%#",str_lat);
First: You should not store numbers as strings. 7.3 and 7.30 are the same values with simply different representations. You should not compare the representations, but the value.
Second: You should not compare floating-point numbers with == but their difference to a delta. In a calculation precision might get lost, rounding is applied and so on. The "mathematical" equal values might be physical different by a more or less small amount.
// remove the zeros from values (if you have them as floats)
NSString *valueFromTheDataBase = [NSString stringWithFormat:#"%g", 34.123450];
NSString *yourValue = [NSString stringWithFormat:#"%g", 34.12345];
if([yourValue isEqualToString:valueFromDataBase]) {
// they are equal
}
OR Make Them floats and compare them
// make them floats and compare them
CGFloat floatFromDB = [valueFromDB floatValue];
CGFloat yourFloat = [yourString floatValue];
if((floatFromDB - yourFloat) == 0) {
// they are equal
}
UPDATED as #Amin Negm says

UISlider limitations in IPhone

In UISlider Control if I set "MinimumValue = 0" & "MaximimValue=2000" then I am not able to set the exact value. On release of slider thumb image it changes the value to + or - 5 to10. My slider width = 225px.
For example:- if I try to set the slider value to 100 then on release of slider thumb it shows me result like 105 or 95.
Code
IBOutlet UISlider * slider;
//"Slider" Object attached using XIB(.NIB)
slider.minimumValue = 0;
slider.maximumValue = 100;
//Attached Below Method Using XIB(.NIB) with "value Changed" event.
-(IBAction)slider_Change:(id)sender;
{
//Assigning value to UITextField
textFiled.text = [NSString stringWithFormat:#"%d",(((int) (slider.value)) * 20)];
}
Because you have set your slider range to be so large each pixel on the screen is equivalent to 8.888.
2000 / 255 = 8.8888888889
To get graduations of 1 you'll need to move the slider 1/8 of a pixel (this is impossible).
Alternatives are:
Make the slider longer
Reduce the slider range
Use a different control UIStepper
In addition to the answer by #rjstelling, it seems as if you are trying to convert the value of the slider to become an integer value. I believe that the default of the slider is a double value, this would cause the value to become rounded as it is increasing, since there will not be decimal values. Unless it is a requirement, see if you can use double.
textFiled.text = [NSString stringWithFormat:#"%f",(slider.value * 20.0)];
or if for some reason you'd want to restrict the level of decimal places you could go, you could do:
textField.text = [NSString stringWithFormat:#"%.1f", slider.value * 20.0];
This will give you the result of a decimal like 1.5 as your answer.
You could do anything with the decimal or even do something like %1.2f, %4.3f, or even %10.4f,
those will produce results like: 0.34, 0012.446, and something extreme like 000000345.1111
Hope this helps

How do I reverse UISlider's min-max values?

Is there a good way to reverse UISlider values? The default is min on the left and max on the right. I'd like it to work the opposite way.
Rotating it 180 degrees seems a bit silly. Any ideas?
Thanks!
Just subtract the value you get from the slider from the maximum value, that will reverse the values.
I then needed the exact same thing as you...so rotated it another 90 degrees placing it in an upside down position. Slider is symetrical, so it looks exactly the same in both positions. But the min and max values are now the opposite.
Command is...
mySlider.transform = CGAffineTransformRotate(mySlider.transform, 180.0/180*M_PI);
you can use the following formula:
let sliderValue = yourSlider.minimumValue + yourSlider.maximumValue - yourSlider.value
Will reverse your values.
Subclass NSSlider/UISlider. Override these two methods thus -
//Assumes minValue not necessarily 0.0
-(double)doubleValue
{
double minVal = [self minValue];
double maxVal = [self maxValue];
double curValue = [super doubleValue];
double reverseVal = maxVal - curValue + minVal;
return reverseVal;
}
-(void)setDoubleValue:(double)aDouble
{
double minVal = [self minValue];
double maxVal = [self maxValue];
double reverseVal = maxVal - aDouble + minVal;
[super setDoubleValue:reverseVal];
}
This will invert the values allowing right/top to appear as minimum and left/bottom to appear as maximum
The accepted answer is incorrect because it assumes that the slider's Min value is 0.
In my case, my Min is 1.1 and Max is 8.0
You need to subtract the slider's value from Min+Max value to inverse the value.
Try this beautiful line -
yourSlider.semanticContentAttribute = .forceRightToLeft
This changes the min-max track colors + invert the values + you are not losing your frame like in the transform answers.
Just subtract slider current value from max value.
label.text = [NSString stringWithFormat:#" %.f%% ", 100 - self.slider.value];
You can simply flip the slider horizontally using transform (Swift 5.0):
slider.transform = CGAffineTransform(scaleX: -1, y: 1);
How about this:
slider.maximumValue = -minimumValue;
slider.minimumValue = -maximumValue;
-(void) sliderChanged:(UISlider *) slider {
float value = -slider.value;
// do something with value
}
Then just use -slider.value.

NSNumber doubleValue 0 doesn't return exactly 0

I'm dealing with an NSMutableArray of NSNumber and I ran into a weird bug. The [NSNumber doubleValue] (construct with double 0) seems to not to be equals to 0.0.
The bug appears in the simpliest function ever : the max function.
- (double) maxValY{
double max = DBL_MIN;
for (NSNumber *doubleNumber in arrayNumbers) {
if (max<[doubleNumber doubleValue]){
max = [doubleNumber doubleValue];
}
}
NSLog(#"max %f",max);
if(max <=0.0){
NSLog(#"max is equal to 0");
return 1;
}else{
NSLog(#"max is not equal to 0");
}
return max;
}
The console prints :
2013-01-02 11:27:56.208 myApp[1920:c07] max 0.000000
2013-01-02 11:27:56.210 myApp[1920:c07] max is not equal to 0
This isn't a bug: it's double precision. With floating point number what you see is not necessarily what you're getting. This question has some more information:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
The easiest solution is going to be to cast to an integer, if all you care about is whether a number is equal or greater than zero. There are other alternatives that are more complex: the answer I've linked to has a few. The link someone gave you in the comments about floating point mathematics may also be helpful.

Resources