-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace usages of long with int32_t / uint32_t and make power()/energy() return int32_t #50
base: master
Are you sure you want to change the base?
Conversation
Change power() and energy() functions to return int32_t instead of float, they don't support decimal values
Hi, @djGrrr. |
Hi @olehs
Here's a quick sketch to demonstrate the above points: int32_t test_int = 0xFFFFFF;
float test_float = 0xFFFFFF;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600u);
Serial.println();
// volts
float v = 120.0;
// amps
float i = 10.0;
// volt-amps
float va = v * i;
// watts
int32_t p = 1050;
// power factor
float pf = p / va;
Serial.print("PF (float var): ");
Serial.println(pf);
Serial.print("PF (calculation): ");
Serial.println(p / va);
}
void loop() {
// put your main code here, to run repeatedly:
test_int++;
test_float++;
Serial.print("int32_t: ");
Serial.print(test_int);
Serial.print(" int32_t(float): ");
Serial.print((float)test_int);
Serial.print(" float: ");
Serial.println(test_float);
delay(1000);
} Which will output:
As you can see, even with the power/watts as int32_t, it has no problem with the power factor calculation. |
I was rather thinking about human mistakes... This change may be non-backward compatible.
Output
|
You are certainly correct about that particular error, but I think the likelihood of such an error with this library is slim to none, because the "i" variable in this case would be float, otherwise the error would have already existed, since the float value from the library would already have been cast to int32_t. float e = pzem.energy(ip); e would be cast to float, even if the return type from energy() is int32_t, therefor no bug would exist. on the other hand, if the code was: int32_t e = pzem.energy(ip); Then the bug would already exist, as the float from the energy() function would be cast to int32_t It seems highly unlikely that pzem.energy(ip), or any of the other functions would be used directly in a calculation without first assigning it to a variable. |
Going back to timeout. |
uint32_t is technically just an alias for unsigned long on most (all?) platforms, that change is more just for consistency with the other integer variables. |
Every other integer type is specified with intXX_t / uintXX_T, but the 32bit type is using "long", this should be changed so that it is consistent, and properly support potentially differing sizes on different platforms.
It also make sense to change the return types of the power() and energy() functions to return int32_t instead of float, they don't support decimal values.