# Unsigned Float SOLVED

Hi,

I need to compare results between two thermometer sensors, if their difference is greater than 2.5 Celsius.
Now I think I have been struggling with this one enough now.
Below is a simple example, one of several versions I have tried to get work, but no. I can’t understand why.
I have tried everything I can think of and browsing forums without solving this one.

All I need is one digital after value. That is a must.

Here are results I get when running the code;

514
216
4294966998

``````float value1 = 5.1415926;
float value2 = 2.168;

void setup()
{
Serial.begin(9600);
}
void loop()
{
int   c = (int) (value1 * 100);
int  d = (int) (value2 * 100);

int  e;
Serial.println(c);
Serial.println(d);
e = d - c;
Serial.println(unsigned (e) );
delay(2000);
}
``````

Why use "int"s?

What’s wrong with fabs?

``````//return true if the different is less than. 2.5
book Compare(float value1, float value2)
{
float temp = value1 - value2;
return (temp >= -2.5 && temp <= 2.5);
}
``````

You subtract a large number from a small number. This gives you a negative result.

Then you tell the software to print a unsigned value. This is not possible.

Integer numbers are stored as Two's complement. Unsigned integers are just binary conversion. Check wikipedia "Two's complement" for more details.

You can get the absolute value using the abs() function.

unsigned int f = abs(e);
Serial.println( f );

TheMemberFormerlyKnownAsAWOL:
Why use "int"s?

What’s wrong with fabs?

``````float x = fabs(value2 - value1);
Serial.println(x, 2);
byte x1 = (byte)(x * 10); //x1 = 0x1D
Serial.print(x1);  //shows: 29 (2.97x10 = 29.7)
if (x1 < 25)  //2.5x10 = 25
{
//------
}
``````

Thanks guys and gals. Now works like a charm.
If someone has similar question/problem, below is the working code.
I did not know about the 'fabs' function until now.
Was really close the solution with 'abs', but probably something was wrong.

Still another question regarding this; I think I never need this, but just in case; this code gives me only two digits. Why, and how to get three for example?

BTW, this code is part for the automated fan which starts when I start cooking. Default running time is 10 mins or some other between 5-30 mins that could be set via Blynk.

float value1 = 5.1415926;
float value2 = 2.168;

void setup()
{
Serial.begin(9600);
}
void loop()
{
float h = fabs(value1-value2);
Serial.println(h);
delay(2000);
}

how to get three for example?

You just have to ask nicely.

``````Serial.println(h, 3);
``````