# day 2 this math problem

ok I cannot for some reason get it in my head what is wrong. My brain just can’t comprehend this problem 2 days into this from reading on the net trying just about everything I can try…

the code, overflow, bits limit, limitations of float ect…???

One of you smart guys know what the problem is and explain how the hardware or code is handing it?

my best guess is it’s overflowing throwing a 0 but I thought float could do 32bit math or is it my code??

I need to get percentage from 1 to 65535 / 65535 * 100 so I can get a percentage of a duty cycle…

I have tried floats and longs because I would need to see 0.392156863 or at least to 0.001 to 100% for a duty cycle. This is the last thing I tried before reaching out here.

`````` float dcperc = ((5637/65535l)*(100));
lcd.print (dcperc);
``````

The other thing I would need is a value in voltage.

``````float vout = ((5)*(step)/(65535));
lcd.print (vout)
``````

I don’t have problems with anything else until I hit this 65535 then for some reason it just doesn’t work except give me a 0.

`float dcperc = ((5637/65535l)*(100));`By default does integer calculation `float dcperc = ((5637.0/655351.0)*(100));`forces the calculation to be done using floats

You probably don't need floating point if two decimals is enough.

ChrisTenone: You probably don't need floating point if two decimals is enough.

isn't float the only way to get decimal though?

and you'll need float or long for 0.001 first step is 3 digits I probably should change that original post..

Then getting voltages is worst 7.62951E-05

Code: [Select]

float dcperc = ((5637/65535l)*(100));

By default does integer calculation Code: [Select]

float dcperc = ((5637.0/655351.0)*(100));

forces the calculation to be done using floats

[/quote]

but you sent me to looking at PIC microcontrollers and they seem to have better answers to the problem. need to use 24bits or 32 bits.

isn't float the only way to get decimal though?

Why are you being sarcastic?

but you sent me to looking at PIC microcontrollers and they seem to have better answers to the problem.

The answers are independent of the processor.

Why are you being sarcastic?

The answers are independent of the processor.

[/quote]

I am not being sarcastic. you are basically telling me maybe I should go to school to help me answer my question.

Have you not read my original question. I have read all this for 2 days and I cannot solve the problem with the information out there.

But then this is the internet where you have 5 million people who cannot answer a question because A either they don't know and would rather post some random stuff to fullfill their post Quota for the day,week,year, and or lifetime then come up with a simple walk though of it.

And as a moderator your role modeling the very problem.

so as a consumer I either come up with a solution or say forget it and look at competitors and thus spread word about what a horrid first time experience asking for help to a problem I have been unable to solve and the very leadership in place that seems to drive things into the dirt.

I have been rather having fun with the Arduino platform for the last few months until now when I actually hit a wall then to ask a question and then belittled by the response.

Edited to deal with your last post:

Dude, do you want to learn how to do this, or just want someone who already knows to tell you the easy answer? The links that CB posted ARE the real stuff. This is the kind of thing you run into regularly when programming Arduino (or any other microprocessor for that matter.) Coding Badly, nor any of the other moderators or participants here are not part of the Arduino company, and (I believe) do not give a rat's ass if you go to the competition or not.

None-the-less, here's some sample code that does what you want with integer math:

``````unsigned long voltage = 5637000UL;
unsigned long percentage = 0UL;
byte sensor = A0;

void setup() {
Serial.begin(9600);
pinMode(sensor, INPUT_PULLUP);
}

void loop() {
// put your main code here, to run repeatedly:

percentage = map(voltage, 0UL, 1024UL, 0UL, 100000UL);
Serial.print(percentage/1000UL);
Serial.print('.');
Serial.println(percentage%1000UL);
}
``````

Coding for PIC is nearly identical with coding for Arduino.

The advantage of doing math with integers is that it's MUCH faster as the Arduino does not have a floating point processor. So it has to break the float down into the mantissa and the exponent, factor the mantissa, then do multiple calculations, and then do any carrying, then rebuild the number as a float. Much more processing. If all you are doing is a voltage calculatio, it's fine, but if you are (say) following a ball around the room, calculating position in real time, it gets to be a bit much.

Another advantage of integers is that any rounding error is limited to one digit.

ps, I fail to see where you were in any way belittled.

ChrisTenone: Coding Badly, nor any of the other moderators or participants here are not part of the Arduino company...

Correct. Uncompensated volunteers.

...and (I believe) do not give a rat's ass if you go to the competition or not.

Now that Atmel has been acquired going to the competition (PIC) is a distinction without a difference. So, I guess, there is no perspective in which you are incorrect.

mdcplas: you are basically telling me maybe I should go to school to help me answer my question.

You are an abject failure at reading my mind. Please stop before you embarrass yourself further.

Have you not read my original question.

Whether or not I have read your original question is irrelevant. I was responding specifically to reply #3.

Hi, mdcplas,

• has your problem been solved?
• did you learn about fixed and floating math?

We are not here to provide you with an instant complete solution, we are here to provide guidance and advice.

This will help you develop your sketch, so that you understand how it works and you can debug any problems that may develop.

Tom.... :)

mdcplas: isn't float the only way to get decimal though?

and you'll need float or long for 0.001 first step is 3 digits I probably should change that original post..

Then getting voltages is worst 7.62951E-05

Initially, one may simplify things and say yes floats means decimals, integers do not. To calculate your percentage, do as follows.

``````    // assuming you have your value between 0 and 65535 in unsigned int variable x
float percentage = (x*100.0) / 65535.0;  // works
int percentage = (x/65535)*100; // does not work
``````

In the code above, the key is to force the compiler to generate code for floating point calculations. Multiplying integer value x by 100.0 does this. Alternatively you could have written more detailed steps.

``````   float valueAsFloat=x;          // example: 4100 becomes 4100.0
float fraction=x/65535.0;     // floating point division
float percentage = fraction * 100.0;  // floating point multiplication
``````

Now, this should give approximately the same result. I say "approximately" since floating point variables are subject to rounding errors. But the calculation is the same as that above.

ChrisTenone: You probably don't need floating point if two decimals is enough.

This is probably not what OP is asking. He needs something that works. The fact that all numbers inside a microprocessor are integers is not relevant here.

We don't have any information yet indicating that processing speed is an issue, and so one uses float.

:-)

mdcplas: I am not being sarcastic. you are basically telling me maybe I should go to school to help me answer my question.

Have you not read my original question. I have read all this for 2 days and I cannot solve the problem with the information out there.

But then this is the internet where you have 5 million people who cannot answer a question because A either they don't know and would rather post some random stuff to fullfill their post Quota for the day,week,year, and or lifetime then come up with a simple walk though of it.

And as a moderator your role modeling the very problem.

so as a consumer I either come up with a solution or say forget it and look at competitors and thus spread word about what a horrid first time experience asking for help to a problem I have been unable to solve and the very leadership in place that seems to drive things into the dirt.

I have been rather having fun with the Arduino platform for the last few months until now when I actually hit a wall then to ask a question and then belittled by the response.

I'm sorry you feel this way. Unfortunately there is a tendency on this forum that newbies get responses that are a bit terse, and not always to the point of what they asked for. Not understanding the answer, the OP (original poster) then does the "mistake" of asking, or worse, assuming a naive position, such as your wondering if floats surely are the only way to produce decimals.

And then all hell breaks loose, with suggestions for reading materials, and other hints at education. The original question is lost.

If I get you correctly, you are a beginner with Arduino and possibly programming. And so I think the responses you got, and how some people on this group get worked up and seemingly offended by close to nothing, is a bit unfair to you.

On the other hand, in the defence of the frequent contributors on this group, it must be said that there is probably 10-50 newbie posters every day. Many of them are rude and expect people on this group to come up with complete solutions to their rather loosely formulated ideas, without even the faintest concept of a solution themselves. Some of them don't seem to read the actual responses they get, and many harbour strange superstitions as to what programming is and is not.

:-)

Now that Atmel has been acquired going to the competition (PIC) is a distinction without a difference. So, I guess, there is no perspective in which you are incorrect.

[/quote]

I think he is talking about support forums ... :-)

float dcperc = ((5637/65535l)(100)); By default does integer calculation float dcperc = ((5637.0/655351.0)(100)); forces the calculation to be done using floats

mdcplas: Glad I didn't ask how to blink an led i'm sure you all have great answers and links for that too. but you sent me to looking at PIC microcontrollers and they seem to have better answers to the problem. need to use 24bits or 32 bits.

I have no idea in what respect you think that this answer that you were given is not a clear and complete answer to your question. Put the .0 on the end. This tells the compiler that it's a floating-point constant, and the compiler will use floating point arithmetic to do the calculation.

One of you smart guys know what the problem is and explain how the hardware or code is handing it?

Mu. It is neither the hardware nor the code: it is the defined behaviour of the C++ programming language. And it isn't wrong or broken: programs quite often rely on the behaviour of integer arithmetic to do things.

Rupert909: This is probably not what OP is asking. He needs something that works. The fact that all numbers inside a microprocessor are integers is not relevant here.

We don't have any information yet indicating that processing speed is an issue, and so one uses float.

:-)

Really? "needs something that works"? Processing speed is only one of the advantages of fixed math over floats.

Your attempt to discredit fixed math by creating bad code that does not work is sophomoric. The OP stated his precision requirement. Fixed point will perform these calculations better than using floating point. If you think my code does not work, try it.

ChrisTenone: Really? "needs something that works"? Processing speed is only one of the advantages of fixed math over floats.

Your attempt to discredit fixed math by creating bad code that does not work is sophomoric. The OP stated his precision requirement. Fixed point will perform these calculations better than using floating point. If you think my code does not work, try it.

Wow.

I didn't know there existed a Church of Fixed point Math, and that speaking favourably about other ways is considered such an offence.

I'm not discrediting fixed point math, other than telling an assumed newbie that the most straight-forward way of doing calculations with decimals, is using floats.

:-)

https://www.arduino.cc/en/Reference/Float

6-7 decimal places it says. That should do it.