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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:00:52+00:00 2026-06-14T02:00:52+00:00

I wrote this function: static bool colorIsEmpty(const Color col) { return (col[0] == 0

  • 0

I wrote this function:

static bool colorIsEmpty(const Color col)
{
    return (col[0] == 0 && col[1] == 0 && col[2] == 0 );
}

where Color is simply a float[3];
the function doesn’t work if col[3] are all 0;
but this works:

if(col[0] == col[1] == col[2] == 0) {
//gets called
}

however gcc gives me warning:

    cColorTest.c:212:5: warning: suggest parentheses around 
comparison in operand of ‘==’ [-Wparentheses]

so it would be nice if that function works,why it doesn’t work?
I mean the function always return false,

  • 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-14T02:00:54+00:00Added an answer on June 14, 2026 at 2:00 am

    The condition

    if(col[0] == col[1] == col[2] == 0) {
    

    is parenthesised

    if(((col[0] == col[1]) == col[2]) == 0) {
    

    so if col[0] == col[1], it checks whether col[2] != 1, and if col[0] != col[1], it checks whether col[2] != 0. I strongly believe that is not what you want.

    static bool colorIsEmpty(const Color col)
    {
        return (col[0] == 0 && col[1] == 0 && col[2] == 0 );
    }
    

    checks whether all three array elements are 0, and if it doesn’t give the expected results, your array elements are not what you expect them to be.

    Depending on your application, you might want an approximate test instead of strict equality. Due to the nature of floating point numbers, it is not uncommon that different computations that would mathematically have the same result leads to different results in floating point arithmetic/calculus. Usually the difference is small (but it can also become huge, for example due to loss of significance/catastrophic cancellation), so often floating point values are compared with a tolerance.

    Perhaps replacing the col[i] == 0 with an approximate zero test, fabsf(col[i]) < epsilon is the right strategy for your purposes (appropriate values of epsilon depend on the use case).

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

Sidebar

Related Questions

I wrote this function to get a pseudo random float between 0 .. 1
I wrote this utility function: public static <T> List<T> pluck(String fieldName, List list) throws
I wrote this function to make columns sortable. I want to rearrange divs based
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get the unread count of google reader items. function
I wrote this function to communicate with an external program. Such program takes input
So I wrote this function that is given possible numbers, and it has to
I wrote this little function to fill a drop down list with data from
Just for fun, I wrote this simple function to reverse a string in Python:
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =

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.