Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:28:21+00:00 2026-06-09T12:28:21+00:00

I am probably missing something obvious (still learning about Objective-C!) but for some reason

  • 0

I am probably missing something obvious (still learning about Objective-C!) but for some reason one of my NSString variables has a null value in my if statement and I don’t know why?

I have even output to NSLog and I still can’t see why it’s behaving like this.

Basically, the user enters an amount in a text field (itemWeight) and this if statement validates the input and displays an alert according to the result. The problem only seems to be when 0.751 is entered, if you enter any other amount (0.750, 0.749, 0.752, 0.753 and so on) it works as expected.

Relevant code samples as follows…

.h file:

@property (strong, nonatomic) IBOutlet UITextField *itemWeight;

.m file:

NSString *rawWeightText = itemWeight.text;
float convertedWeightText = rawWeightText.floatValue;

NSString *weightMessage;

if (convertedWeightText <= 0.750)
{
    weightMessage = @"under 0.750";
}
else if (convertedWeightText >= 0.751)
{
    weightMessage = @"0.751 or over";
}

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: @"Error"
                      message: weightMessage
                      delegate: nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
[alert show];

Any ideas where I’m going wrong/what I’ve forgotten to do would be much appreciated, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:28:22+00:00Added an answer on June 9, 2026 at 12:28 pm

    Looking at the condition of your if-else if code does not have the range of from 0.750 to 0.751.

    floating-point values ​​must be careful to compare. you should consider about 0.751 possibility 0.75099999...

    the following loop, the result is 99.999046, not 100. Continue to add more accuracy is poor.

    float a= 0.1f;
    float result = 0.f;
    for(int i = 0; i<1000; i++)
    {
        result += 0.1f;
    }
    
    printf("result:%f", result); //99.999046
    

    So, In General, Comparison of these expressions is not recommended.

    if (result == expectedResult)
    

    Writing the following method is recommended to compare.

    bool AlmostEqualRelative(float A, float B, float maxRelativeError)
    {
        if (A == B)
            return true;
        float relativeError = fabs((A - B) / B);
        if (relativeError <= maxRelativeError)
            return true;
        return false;
    }
    

    For more information, please read here

    http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    also, you must read this wiki:

    http://en.wikipedia.org/wiki/Floating_point

    http://en.wikipedia.org/wiki/Denormal_number

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm probably missing something obvious or going about this in the wrong way, but
I'm probably missing something obvious here but here's what I'm trying to do. From
I am probably missing something obvious, but I am getting a 'Object reference not
This is with SBCL 1.0.55 on Debian squeeze. I'm probably missing something obvious, but
Probably missing something completely obvious here, but here goes. I'm starting out with Spring
Probably I'm just missing something obvious, but I can't get the image in my
This is probably an easy one, but I'm missing something I guess. The problem
I am probably missing something obvious but anyway: When you import a package like
OK, I'm probably missing something obvious here, but my searches aren't turning up anything
I'm using Visual Studio 2008 Pro. I'm probably missing something very obvious here, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.