Page 1 of 1

Multiply by 100?

Posted: Thu Apr 06, 2017 3:38 pm
by Jan Lichtenbelt
I try to calculate the percentage of A compared to B. In the simulation I get the right answer of 98%, but with the 16LF1847 completely wrong result.
Who is that possible. And what to do?
Test_ULong_V1.fcfx
(7.41 KiB) Downloaded 360 times
Results
Results
Test_ULong_V1.jpg (61.32 KiB) Viewed 5517 times
With kind regards

Jan Lichtenbelt

Re: Multiply by 100?

Posted: Thu Apr 06, 2017 4:36 pm
by Benj
Hi Jan,

Is that the right program in the attachment?

Re: Multiply by 100?

Posted: Thu Apr 06, 2017 8:44 pm
by Jan Lichtenbelt
Hi Ben,

Sorry but I used the wrong file. Plaes find the Test_Ulong_V1 above.

Jan

Re: Multiply by 100?

Posted: Fri Apr 07, 2017 10:22 am
by Benj
Hi Jan,

The calculation D = 100 * B is what is causing the problem.

If you instead change to this then it should work as expected.

D = B
D = D * 100

This forces the * 100 to be a 32-bit calculation.

If you use the PrintFormattedNumber function to print out the D variable then you will see the true 32-bit value on the LCD - 590000.

Re: Multiply by 100?

Posted: Fri Apr 07, 2017 11:06 am
by Jan Lichtenbelt
Hi Ben

Thanks a lot. It works.

Multiplying by a number ( eg. 100) is no problem if one uses Uint. But is seems that in case of Ulong the number has to be changed first into a Ulong variable.


Kind regards

Jan Lichtenbelt

Re: Multiply by 100?

Posted: Fri Apr 07, 2017 11:31 am
by medelec35
Hi Jan,
One thing I have noticed.
If you want to display correct 590000 instead of 176 then instead of using LCD

Code: Select all

PrintNumber
you can use

Code: Select all

 PrintFormattedNumber
.
Use 1 in the Bool input box for Unsigned number.

Martin

Re: Multiply by 100?

Posted: Fri Apr 07, 2017 12:12 pm
by LeighM
Multiplying by a number ( eg. 100) is no problem if one uses Uint. But is seems that in case of Ulong the number has to be changed first into a Ulong variable.
Yes, it's a "C" thing. It depends upon the size of an "int" on the platform/processor.
So it isn't a problem on say ARM, which is 32 bit.
For interesting reading search "integer promotion" :lol: