# [Solved] Bad mathematic with big number. old : Probleme modulo.

Hello,

I’m having some troubles while using % (modulo), but i think the probleme isn’t modulo. I’m wrong somewhere… And need help cause i’m not usual with c++.

Answer: The probleme was in the first line :
unsigned long long myTime = 233600000+401000;
should be :
unsigned long long myTime = 233600000UL+401000UL;

6v6gt:
If you are using unsigned long numbers eg 40000, you should write these with a following UL eg 40000UL to ensure these are not treated as ints.

Main

``````#include "Test.h"

//Init of the Object Instant
//with one hour and 1 sec, (total in millisecond).
unsigned long long myTime = 23*3600000+32*1000;
Instant instant = Instant(myTime);

void setup() {
//moniteur serie
Serial.begin(9600);
}

void loop() {
unsigned long heure;
int minute;
int seconde;
int milli;

//Take back the time from Instant::getTime(int&,int&,int&,int&).
instant.getTime(heure,minute,seconde,milli);

Serial.println("Start");
delay(500);
Serial.println(heure);
delay(500);
Serial.println(minute);
delay(500);
Serial.println(seconde);
delay(500);
Serial.println(milli);

delay(5000);
}
``````

Test.cpp

``````#include "Test.h"

Instant::Instant(unsigned long long _millis) {
totalMilli = _millis;
UpdateTimeFromMillis();
};

void Instant::getTime(unsigned long &_heure,int &_minute,int &_seconde,int &_milli){
_heure = heure;
_minute = minute;
_seconde = seconde;
_milli = milli;
};

void Instant::UpdateTimeFromMillis(){
heure = totalMilli/1000/60/60%24;
minute = totalMilli/1000/60%60;
seconde = totalMilli/1000%60;
milli = totalMilli%1000;
}
``````

Test.h

``````#ifndef Test_H
#define Test_H

class Instant {

public:
Instant(unsigned long long _millis);

void getTime(unsigned long &_heure,int &_minute,int &_seconde,int &_milli);

private:
unsigned long heure;
int minute;
int seconde;
int milli;
unsigned long long totalMilli;
void UpdateTimeFromMillis();
void UpdateTimeFromHMS();
};

#endif
``````

Output OK :

``````Start
23
0
32
0
``````

This code work… But if (in main, first lign) i do :

``````//this won't work :
myTime = 24*3600000+32*1000;
//output :
//0
//0
//32
//0
``````
``````//this won't work :
myTime = 25*3600000+32*1000;
//output :
//1
//0
//32
//0
``````
``````//this won't work :
myTime = 26*3600000+32*1000;
//output :
//2
//0
//32
//0
``````

As you can see the number of hour is something like reset at 24. But that’s not the same for secondes :

``````//this won't work :
myTime = 23*3600000+33*1000;
//output :
//22
//59
//27
//464
``````

I think there is something i don’t understand, maybe in the “UpdateTimeFromMillis()”.

`  minute = totalMilli/1000/60%60;`What is the order of precedence in that calculation (and others) ? Will the modulo calculation be done at the end of the calculation or at the beginning ? If the latter then 60%60 will always be zero.

The modulo is done after the rest, else the code shouldn't work at all and it work (with not to high value.)

Well... while i copy my code and make it shorter for this forum, i didn't add days possibility, but i forgot to delete %24 at hour line. The probleme stay the same.

After some more try, i think a probleme is in the first line : if i do:

``````unsigned long long myTime = 0*3600000+40*1000;
Instant instant = Instant(myTime);
``````

i got some random answer... but if i do :

``````Instant instant = Instant(40000);
``````

That work, so the probleme is in myTime, maybe the "*" caracter is show as hexadecimal or something like this.

If you are using unsigned long numbers eg 40000, you should write these with a following UL eg 40000UL to ensure these are not treated as ints.

If you have doubts about the execution order of operators, use brackets ( ) to force the order.

``````unsigned long long myTime = 0*3600000+40*1000;
``````

What value do you get in myTime?

What value do you get in myTime?

An enormous value (in milliseconde), i cannot directly log it but that represent:

``````3050584830 days
14 hour
25 min
26 sec
80 millisec
``````

I can see a "warning: integer overflow in expression" soo maybe that is not the correct answer.

you should write these with a following UL eg 40000UL to ensure these are not treated as ints.

Perfect, that solved the problem, can i have more details about that (or a link)?