How to Check if double is zero?

Posted on 2005-04-08
Last Modified: 2012-05-05
I have a variable:

double aVar;

aVar can have any value such as 0.0, 0.000, 0.0007,0.0000003

How can I check if it is zero?

"If (aVar ==0)" does not seem to work
Question by:rohgan
    LVL 30

    Expert Comment

    Hi rohgan,
    A double and a float are not exact numbers.
    You can check to see if the number is between a given range that is close to zero.

    if (aVar < 0.000001f && aVar > 0.000000f)

    David Maisonave :-)
    LVL 45

    Accepted Solution

    Hi rohgan,

    Actually, the statement:

      if (aVar == 0)

    DOES check that the value is zero.  Unfortunately, the inherent error in floating point math algorithms sometimes results in answers that are "almost zero" instead of zero.  (It's kind of like being almost dead or almost pregnant.)  But in C, you have to deal with it.

    Axter gives you a pretty good handle on how to test for "almost zero".  You can also write it as a macro, which can make your code easier to read.

    #define isfpzero(a) (a < 0.000001f && a > -0.000001f : 1 : 0)

      if (isfpzero(aVar))
        printf ("Floating Point Zero");
        printf ("Non Zero");

    Good Luck!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now