Arduino division problems

Thread Starter

Nicholas

Joined Mar 24, 2005
139
Hi guys!

I need to use an Arduino to do some simple arithmetics, but it's not that simple :)

As an example, if I want to divide an integer with an exponent, the Arduino does not seem
to like it: 245/10^-3

I take it that this is due to one number being an integer and the other being a float? The division should
return 245000 (2.45*10^5), but how do I do this in C++/Arduino? The numerator will always be an integer,
and the denominator will always be a float in scientific notation.

Thanks for reading!
Nicholas
 

Picbuster

Joined Dec 2, 2013
990
all values in a calculation should have the same dimension ( float or int or double ...........).
float Result = (float)(value)/0.0001234
Result is a float.

However is you use always a integer multiple or division by 10, 100, 1000.............1000000
Then you may use the trick of handle the value as a string and place the separator.
Yes it's stinky but lack of memory might force you to do this some times BUT try to avoid it.

Picbuster
 

xox

Joined Sep 8, 2017
382
AFAIK that isn't a valid form of scientific notation that you're using (the caret symbol "^" means bitwise XOR). Try instead:

245/1e-3

And C/C++ will always convert the result to floating point as long as one of the operands is a float or double so a cast wouldn't be necessary in such a case.
 

djsfantasi

Joined Apr 11, 2010
5,955
Hi guys!

I need to use an Arduino to do some simple arithmetics, but it's not that simple :)

As an example, if I want to divide an integer with an exponent, the Arduino does not seem
to like it: 245/10^-3

I take it that this is due to one number being an integer and the other being a float? The division should
return 245000 (2.45*10^5), but how do I do this in C++/Arduino? The numerator will always be an integer,
and the denominator will always be a float in scientific notation.

Thanks for reading!
Nicholas
Picbuster’s approach is the correct answer. Strictly speaking, the parens around “value” are not necessary. “(float)” is a “cast” operator. This syntax can be used to change variables of one type to another in a calculation. The Arduino also has functions to do the same. For example, I could use the following line:

float Result = float(intValue) / 10.0^-3;

The initial float data type definition is not necessary, if the variable “Result” has been previously defined as type float.
 

xox

Joined Sep 8, 2017
382
Picbuster’s approach is the correct answer. Strictly speaking, the parens around “value” are not necessary. “(float)” is a “cast” operator. This syntax can be used to change variables of one type to another in a calculation. The Arduino also has functions to do the same. For example, I could use the following line:

float Result = float(intValue) / 10.0^-3;

The initial float data type definition is not necessary, if the variable “Result” has been previously defined as type float.
Could you provide a link to that language extension? It doesn't mention anything about it here.
 

MrChips

Joined Oct 2, 2009
19,923
As a general rule I don't do float on embedded systems.
It would pay to do some preliminary math on values so that one can anticipate the range of values required.

For example, if using 16-bit signed integers 245/1000 would result in a value of 0.
245 x 1000 would result in overflow.

Floating point arithmetic is often a waste of computer resources.
For example, converting temperature °C to °F using the formula F = C * 9 / 5 + 32 is easily and efficiently performed using fixed-point arithmetic.

The same is true for the inverse conversion C = (F - 32) * 5 / 9.
 

Thread Starter

Nicholas

Joined Mar 24, 2005
139
Hi, thanks for your answers! I was just thinking clearly for one second! I can just the decimal 'version' of the scientific
notation, ie 10^-1 is 0.1 and 10^-2 is 0.01. I just need a set number of them:
0.1
0.01
0.001
0.0001
0.00001
0.000001

Such numbers are also floats, yes?
 

MrChips

Joined Oct 2, 2009
19,923
Hi, thanks for your answers! I was just thinking clearly for one second! I can just the decimal 'version' of the scientific
notation, ie 10^-1 is 0.1 and 10^-2 is 0.01. I just need a set number of them:
0.1
0.01
0.001
0.0001
0.00001
0.000001

Such numbers are also floats, yes?
They don't have to be float.
0.1 = 1/10
0.01 = 1/100
0.001 = 1/1000
0.0001 = 1/10000
0.00001 = 1/100000
0.000001 = 1/1000000

If I need to divide by 0.01 I simply multiply by 100. No float required.

You need to tell us the context of your arithmetic. What are you trying to do?
If you are performing calculations that require 1ppm resolution you will have to go to long int data type (i.e. 32-bit integer), or even 64-bit if required.
 

shteii01

Joined Feb 19, 2010
4,647
They don't have to be float.
0.1 = 1/10
0.01 = 1/100
0.001 = 1/1000
If I need to divide by 0.01 I simply multiply by 100. No float required.

You need to tell us the context of your arithmetic. What are you trying to do?
If you are performing calculations that require 1ppm resolution you will have to go to long int data type (i.e. 32-bit integer), or even 64-bit if required.
I emphasized it in red. We don't need the context so don't waste the time. I am seeing in OP tunnel vision and lack of experience. Since I share both qualities with OP, I can relate. OP, like MrChips showed, do it the smart way and stop bashing your head against brick wall.
 
Top