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 942561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:17:32+00:00 2026-05-15T22:17:32+00:00

I have to check a float variable to determine the existence of a particular

  • 0

I have to check a float variable to determine the existence of a particular variable. Ideally it should have been a bool value or at least an int. Because of interface constraints and legacy code I am not able to change the data type to bool. So if I’ve to make a check, can I directly do it as shown below in the sample code:

    void check(float f)
    {
        if (!f)
            printf ("Zero val!\n");
        else
            printf ("Value exists!\n");
    }

Would it be fool proof Or is there be a better way to do it, considering the fact how the value is stored in a float variable. Casting it to an short and checking is out of the game because :
If the value is ‘1’ it is stored as 0.9999998; casting it to a short would result in a ‘0’ which is wrong.

P.S The code happens to be present in a Objective C file, but in the C format.

  • 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-05-15T22:17:32+00:00Added an answer on May 15, 2026 at 10:17 pm

    What you have will work, with the slight caveat that it will detect both positive and negative zero as being zero values. That may or may not matter for your purposes. If you are running on a platform that doesn’t have the default IEEE-754 behavior (like many embedded platforms), then denormals may also pass the !f check, depending on how your compiler handles it. This is probably not a problem for you, but it’s something to be aware of.

    If you must do this sort of thing in the future, it’s probably a better idea to use “not a number” to indicate uninitialized float data, which you can then check for with either (f != f) or isnan(f).

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

Sidebar

Related Questions

I have to check if a value matches a certain string, and the input
I need to have a loop that can constantly check for this variable: NSString
I have a quite simple task: I must check in wchich group my float
I have a method which returns a bool value, with several exit points. However,
How to check if string contains numbers in Python? I have a variable which
Is it possible to check if a variable (the variable can be a float,
I have an array of float values and want the value and more importantly
INSERT MODE i have check box in one tr. and another dropdown control in
I have to check some code and run it. I have the URL: svn+ssh://myuser@www.myclient.com/home/svn/project/trunk
I have to check a number if it satisfies the following criteria: in binary,

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.