I want to know meaning of simple code please

the is peace of code i do not understand its meaning
if (this->_neutralVoltage == float() || isnan(this->_neutralVoltage))

here he is checking value to be equal to float() what is that means ? is that means if its float value this if condition will be true ?

See Value-initialization - cppreference.com and https://en.cppreference.com/w/cpp/language/zero_initialization :

T ()

If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.

So float() is the same as float(0).

Not sure why the author didn't simply write 0 or 0.f.

2 Likes

so that means if the value of float is zero this condition is true ?

Yes, if the value of the member variable this->_neutralVoltage is zero (or NaN), the condition will be true.

1 Like

That float() is zero initialized is new for me as well. In my opinion, programming is also making clear what is going on.

I think I found the origin here: DFRobot_ESP_PH_BY_GREENPONIK/DFRobot_ESP_PH.cpp at master · GreenPonik/DFRobot_ESP_PH_BY_GREENPONIK · GitHub

Perhaps it is a mistake and it was a check if the number is a float ?

The goal is to check if a valid float number was read from EEPROM.
It does not make sense to me, to store a float number but it may not be 0.0

The original code is from DFRobot. They have:

if(EEPROM.read(PHVALUEADDR)==0xFF && EEPROM.read(PHVALUEADDR+1)==0xFF && EEPROM.read(PHVALUEADDR+2)==0xFF && EEPROM.read(PHVALUEADDR+3)==0xFF){

That I understand :smiley:

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.