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?
With kind regards
Jan Lichtenbelt
Multiply by 100?
Moderator: Benj
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Multiply by 100?
Last edited by Jan Lichtenbelt on Thu Apr 06, 2017 6:21 pm, edited 1 time in total.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Multiply by 100?
Hi Jan,
Is that the right program in the attachment?
Is that the right program in the attachment?
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Multiply by 100?
Hi Ben,
Sorry but I used the wrong file. Plaes find the Test_Ulong_V1 above.
Jan
Sorry but I used the wrong file. Plaes find the Test_Ulong_V1 above.
Jan
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Multiply by 100?
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Multiply by 100?
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Multiply by 100?
Hi Jan,
One thing I have noticed.
If you want to display correct 590000 instead of 176 then instead of using LCD you can use .
Use 1 in the Bool input box for Unsigned number.
Martin
One thing I have noticed.
If you want to display correct 590000 instead of 176 then instead of using LCD
Code: Select all
PrintNumber
Code: Select all
PrintFormattedNumber
Use 1 in the Bool input box for Unsigned number.
Martin
Martin
- LeighM
- Matrix Staff
- Posts: 2178
- Joined: Tue Jan 17, 2012 10:07 am
- Has thanked: 481 times
- Been thanked: 699 times
Re: Multiply by 100?
Yes, it's a "C" thing. It depends upon the size of an "int" on the platform/processor.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.
So it isn't a problem on say ARM, which is 32 bit.
For interesting reading search "integer promotion"
