Simulation calculations.
If using the following formula:
Volts= 920 *5/(1023/100)*3
Using PIC Maths answer Should be 1380
Flowcode simulation displays answer as 1348
Is it possible to get the simulator to ignore rounding up and down and any deals with whole numbers?
I have recently found out this can have a dramatic effect on PWM etc.
Also It would be good if a calculator function can be inbuilt to Flowcode (or even a separate calculator), which also does not round up or down, but only deals with whole numbers just like microcontrollers do.
Change in simulation calculations.
Moderator: Benj
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Change in simulation calculations.
Using a calculation like this can have other problems. The C language does not specify the order of evaluation of expressions and "*" and "/" have the same precedence. You could use additional brackets to foruce the order of evaluation, or better still you could break the calculation into separate lines.
If you provide a simpler example where the calculations in simulation and "real life" are different, I will look into this and try to sort it.
If you provide a simpler example where the calculations in simulation and "real life" are different, I will look into this and try to sort it.
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Change in simulation calculations.
Thanks Steve.
Here is a different example using Duty set for int:
Duty = (151/14)*800
Results should be the same with or without brackets
Simulation will show 8628 but the true result would be 8000
Reason for this is simulation will do (10.785)*800 = 8628
Where as a microcontroller will do (10)*800=800= 8000
I realise I have used constants, but in the real world the 151, 14 and 800 could be all variables.
Here is a different example using Duty set for int:
Duty = (151/14)*800
Results should be the same with or without brackets
Simulation will show 8628 but the true result would be 8000
Reason for this is simulation will do (10.785)*800 = 8628
Where as a microcontroller will do (10)*800=800= 8000
I realise I have used constants, but in the real world the 151, 14 and 800 could be all variables.
Martin
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Change in simulation calculations.
I have discovered the free calculator that ships with windows7 has a 'Programmer' mode. If Qword is selected, then If the above equation is entered into the calculator then result is = 8000 and not 8628
If you have not got windows 7, then calculator can be downloaded from here:
http://www.softpedia.com/progDownload/W ... 22549.html
This site is reputable, and calculator is not shareware, but freeware, so is legal to use.
If you have not got windows 7, then calculator can be downloaded from here:
http://www.softpedia.com/progDownload/W ... 22549.html
This site is reputable, and calculator is not shareware, but freeware, so is legal to use.
Martin
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Change in simulation calculations.
Instead of doing this:
Duty = (151/14)*800
you should perform the multiplication first as this will retain a higher level of precision:
Duty = (151*800)/14
Duty = (151/14)*800
you should perform the multiplication first as this will retain a higher level of precision:
Duty = (151*800)/14
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Change in simulation calculations.
Thanks Steve, But I though 151*800 = 120800 would be higher than 32767 so woulld roll over?
I thought that even if using bytes, microcontrollers use 16bit maths when calculating?
If Im worng, then fair enough I stand corrected.
I thought that even if using bytes, microcontrollers use 16bit maths when calculating?
If Im worng, then fair enough I stand corrected.
Martin
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Change in simulation calculations.
Then floating point variables may be your only option. Going back to your example, 151/14 = 10.785, which will be truncated to 10 by the maths used in the micro. Your "real" answer should be 8628.57, but the micro will use 10 and calculate it as 8000 (which is quite a significant difference).
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Change in simulation calculations.
Steve, I totally agree with what your saying. Hence my first post was not about finding alternatives, but for the simulation to show 8000 and not so people don’t get caught out in this way.steve wrote: Going back to your example, 151/14 = 10.785, which will be truncated to 10 by the maths used in the micro. Your "real" answer should be 8628.57, but the micro will use 10 and calculate it as 8000 (which is quite a significant difference).
Then the simulator will match what the answer would be if calculated within the micro, rather that showing a difference in results.medelec35 wrote:If using the following formula:
Volts= 920 *5/(1023/100)*3
Using PIC Maths answer Should be 1380
Flowcode simulation displays answer as 1348
Is it possible to get the simulator to ignore rounding up and down and any deals with whole numbers?
So all I'm saying is would it be a good idea for the simulator to show the 8000 and not 8628?
Martin
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Change in simulation calculations.
Yep - I understand (although in truth it might be too difficult to "fix" the simulation in this way).
But I still thinks it's important that I highlight the pitfalls associated with integer mathematics.
But I still thinks it's important that I highlight the pitfalls associated with integer mathematics.