Bigger number addition not working in Arduino

Thread Starter

embpic

Joined May 29, 2013
189
I am using a water flow sensor and i want to keep track of flow amount for a month. So for that i am adding it the variable which of uint64_t type.
Now addition works for uint32_t datatypes but does not work for uint64_t data types.

is there any snippet or algorithm which can add uint64_t datatypes.

i tried one of the library called BigNumber but it takes time to calculate and hangs the count. which won't work for my project.
 

MrSalts

Joined Apr 2, 2020
1,762
I am using a water flow sensor and i want to keep track of flow amount for a month. So for that i am adding it the variable which of uint64_t type.
Now addition works for uint32_t datatypes but does not work for uint64_t data types.

is there any snippet or algorithm which can add uint64_t datatypes.

i tried one of the library called BigNumber but it takes time to calculate and hangs the count. which won't work for my project.
No, 64 bit takes time on an 8-bit processor. You can try a different Device that the Arduino-IDE can program like an ESP32 but, that may not work with the large number library because that library wasn't written with an ESP32 in mind (as far as I know). The good news is that an ESP32 is about 5x faster processor speed as well so that may help you.
You can also try taking fewer data samples (longer time delay) larger volume scale, or what ever can get your accuracy to where you need it with a standard device.
 

Sensacell

Joined Jun 19, 2012
2,998
Break the addition into smaller blocks.


increment a single byte, when it rolls over, increment the second and so on.
This will lower the processor bandwidth required a lot.

re-organize your algorithm to avoid doing the monster 64 bit math every cycle.

Funny how easy this would be in assembly, high level languages encourage waste of CPU cycles
 

ericgibbs

Joined Jan 29, 2010
15,516
hi emb,
What level of resolution in units of water/rate are you using, [gallons, litres] and what is the expected maximum volume of in one month.?

What is the water flow sensor type or datasheet.?

E
 

Thread Starter

embpic

Joined May 29, 2013
189
Break the addition into smaller blocks.


increment a single byte, when it rolls over, increment the second and so on.
This will lower the processor bandwidth required a lot.

re-organize your algorithm to avoid doing the monster 64 bit math every cycle.

Funny how easy this would be in assembly, high level languages encourage waste of CPU cycles
i was trying the same.
 

Thread Starter

embpic

Joined May 29, 2013
189
i am adding value at every 100ms to show the flow. and i want to show the total amount of water flowed for 2 months.
 

MrSalts

Joined Apr 2, 2020
1,762
i am adding value at every 100ms to show the flow. and i want to show the total amount of water flowed for 2 months.
Could you take less frequent readings? Could you use a different unit of measure to make each increment smaller?

to do that many data collections, you need your average value to be less than 82 to avoid overflowing before 2-months. Or, stay under 820 if you take 1 reading per second.
 

BobTPH

Joined Jun 5, 2013
4,897
You could also accumulate, say 1000 readings, divide that by 1000 then add that average to the 2-month accumulator.

Bob
 

drjohsmith

Joined Dec 13, 2021
394
Follow on from @BobTPH idea
make an average every hour / day
and put that into an array
gives you more information
BUT
That is a get around, / engineering solution
Wonder why its not possible to use int_64 addition !

I do do similar in products
but I use the https://www.pjrc.com/store/teensy40.html
which is not 8 bits based.

One other thing that comes to mind
The unit_64 was "problematic" , especially in printing,
so it could just be the print out thats wrong !

https://www.paleotechnologist.net/?p=4611
I thought it was fixed, but may be not !
 

ApacheKid

Joined Jan 12, 2015
688
I am using a water flow sensor and i want to keep track of flow amount for a month. So for that i am adding it the variable which of uint64_t type.
Now addition works for uint32_t datatypes but does not work for uint64_t data types.

is there any snippet or algorithm which can add uint64_t datatypes.

i tried one of the library called BigNumber but it takes time to calculate and hangs the count. which won't work for my project.
I'm not familiar with the platform or the specifics of what you're doing but this sounds all wrong. If the compiler is accepting the type uint64_t then the code it generates should fully support arithmetic operations on variables of that type.

I may have missed something, but that's my initial reaction, is there a compiler bug? Can you show us the generated code for a uint64_t being incremented? what target machine instructions does it generate?
 

djsfantasi

Joined Apr 11, 2010
8,323
I'm not familiar with the platform or the specifics of what you're doing but this sounds all wrong. If the compiler is accepting the type uint64_t then the code it generates should fully support arithmetic operations on variables of that type.

I may have missed something, but that's my initial reaction, is there a compiler bug? Can you show us the generated code for a uint64_t being incremented? what target machine instructions does it generate?
I didn’t see anywhere that addition is not supported on variables of type uint64_t. Nor any indication that a compiler bug is intimated.

The problem that the TS is experiencing seems to be that the addition operation is taking too long for the time available.

Am I wrong?
 

MrSalts

Joined Apr 2, 2020
1,762
I didn’t see anywhere that addition is not supported on variables of type uint64_t. Nor any indication that a compiler bug is intimated.

The problem that the TS is experiencing seems to be that the addition operation is taking too long for the time available.

Am I wrong?
You are right.
Another poster is wrong assuming that an error is not generated when a 64-bit variable is declared in Arduino IDE.

When using the "big number library" the OP claimed addition takes too long. Arduino IDE does not support beyond 32-but unsigned. No matter what was said above, a special (slow) library is needed.
 

ApacheKid

Joined Jan 12, 2015
688
I didn’t see anywhere that addition is not supported on variables of type uint64_t. Nor any indication that a compiler bug is intimated.

The problem that the TS is experiencing seems to be that the addition operation is taking too long for the time available.

Am I wrong?
The OP said this "... but does not work for uint64_t data types." which I took to mean it generates an incorrect result.
 

djsfantasi

Joined Apr 11, 2010
8,323
The OP said this "... but does not work for uint64_t data types." which I took to mean it generates an incorrect result.
I took “does not work” and “is not supported” to be two different statements. He then goes on in the same post to state…
but it takes time to calculate and hangs the count. which won't work for my project.
I took this additional information to suggest the the uint_64 addition takes too long, to produce the correct result, for his program. That - the timing - is what doesn’t work, not the addition itself.
 

MrSalts

Joined Apr 2, 2020
1,762
The OP said this "... but does not work for uint64_t data types." which I took to mean it generates an incorrect result.
I took “does not work” and “is not supported” to be two different statements. He then goes on in the same post to state…


I took this additional information to suggest the the uint_64 addition takes too long, to produce the correct result, for his program. That - the timing - is what doesn’t work, not the addition itself.
The uint64_t is not supported by Arduino IDE.
The comment about being too slow in 64-bit is a special large number library known as, BigNumber, which handles values larger than 32-bits without creating a 64-bit variable. This library is too slow to allow a sample every 0.1seconds.
 
Top