Data type problem

I am having a problem with an equation. The equation is hm=(h+m/60). h and m are both integers. I am declaring hm as a float because the result would be a decimal number. The problem is the print out gives only the value of h. Here is the code;

 Serial.println((rtc.hour) ());
     Serial.println((rtc.minute) ());
     h=((rtc.hour) ());
     m=((rtc.minute) ());
     hm=(h+m/60); 
     Serial.println(hm);

It uploads ok. If h=9 and m=44 the sum should be 9+0.733 or 9.733. all I am getting is 9.
What is wrong with the coding? This may be a data type problem or something else.

Division will occur before addition. Use brackets for priority addition.

The math is processed left to right. So, h+m is added, then divided by 60. If you want another order to be done, use more parenthesis.

And use 60.0 for the divisor to get a fractional answer.

The division will be done first, then the addition, but all the math is done as integers.
44/60 gives a result of 0, which when added to 9 gives a total of 9.
The conversion to floating point is only done after everything in the parenthesis is complete.

Why change hours and minutes to float hours?
If you want to do calculations, convert to seconds and work with seconds!

h * 3600 + m * 60 = s.

hours = seconds / 3600 with remainder of seconds % 3600.
minutes = remaining seconds / 60, remaining seconds by % 60.

Calculate in seconds gives you more precision in divides.

Floating point on Arduino is for people who are bad at math!

2 Likes

+1.

Many ppl use seconds since midnight.

Seconds since the beginning of time, 1 JAN 1970 is handled by RTC libraries, some at least, and another library someone will name, I'm sieved out on it right now.

a7

1 Like

have a look at C/C++ operator_precedence and and associativity

e.g. in

h+m/60

division is higher precedence than + therefore division is done first then +, e.g.

h+(m/60)

also if m is less than 60 the result of the integer division would be 0
therefore make the calculation floating point

hm=h+(m/60.0)

what type is hm (no sign of a declaration in your code fragment) ?
if it is integer any factional component of h+(m/60.0) would be lost, e.g. 6.50 becomes 6

see implicit_conversion

That system is officially known as Unix Time! IIRC Unix was launched at 0!

I used it in the 90's. A log began every line with Unix Time in 8 char HEX. Unix Time was supposed to get an upgrade, it runs out on Jan 19, 2038 because it uses a signed 32-bit value but that does reach back 68 years as well! It's possible they will simply make a 64 bit version to cover seconds past the Sun becoming a Red Giant!

1 Like

not correct Paul. division and multiplication take priority over + and -. This equation works now. One very simple change. hm=h+m/60.0. If you don't add the zero m/60 is taken as an integer calculation. This makes it a floating point calculation.

fixed by doing this. 44/60.0. then the maths is done as a floating point not integer.

very good idea. gives more precision. but even without seconds the maximum error will be 1 minute.

hm was earlier declared as a floating point. your suggestion fixes the problem. you don't need the brackets around m/60.0 because division takes precedence over addition.

He put the parenthesis there for e.g. making the point clear.
When that is done in source code it is for making intention clear so nobody gets ideas later on.

Where did you get this weird syntax?

Don't you understand how to use functions in C?
Why not just like this:

     h= rtc.hour();
     m= rtc.minute();
1 Like

==>

h = rtc.hour();
m = rtc.minute();
1 Like

There are units for work and units for display.

Floating point is slow and loses precision, so why use float?

Work in seconds, lose a fraction of a second.
Work in millis, use bigger numbers so what?
Arduino has millis 32-bit good for over 49 days.

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