A
A
AlexTutorialYT2021-12-03 18:50:58
Arduino
AlexTutorialYT, 2021-12-03 18:50:58

How to compare float arduino?

I am writing a power supply control system. I need to compare two float numbers: the voltage setting and the current voltage.

if(this_voltage <= voltage){
// Подкручиваем напряжение
  }

The code is not working correctly. I read that float in the conditions is crooked. How can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Skorzhinsky, 2022-01-11
@AndyKorg

The simplest solution is to reduce to an integer with the required accuracy.
Something like this (perhaps the bit depth of the integer will need to be increased):

#define ACCURACY 100.0
if (((int) (this_voltage*ACCURACY))<= ((int) (voltag*ACCURACY))){
// Подкручиваем напряжение
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question