If I make a calculation of say cm to inches where 1 inch = 2.54 cm what happens to rounding? Would it return 3 or 2 for the amount of cm in one inch? Does Sourceboost round any value or just chop off the effective fractional value/ decimal point?
If it chops it off presumably adding 1/2 to the answer would provide 5/4 rounding. Such as cm = ((inch * 25.4) +5)/10
Rounding of values
- 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:
Hello
Yes the rounding done by the PICmicro is such that the value to the right of the decimal point is completeley lost.
eg 9.99 would be read as 9
In your example for supplying 5/4 rounding
cm = ((inch * 25.4) + 5 ) / 10
this would actually be
cm = ((inch * 25) +5 ) / 10
If you need to maintain the decimal point then you can either :
Times everything by 10 which will give you one decimal place.
Times everything by 100 which will give you two decimal places.
Use the Floating point library available for Flowcode which will give you a 32 bit floating point number.
Yes the rounding done by the PICmicro is such that the value to the right of the decimal point is completeley lost.
eg 9.99 would be read as 9
In your example for supplying 5/4 rounding
cm = ((inch * 25.4) + 5 ) / 10
this would actually be
cm = ((inch * 25) +5 ) / 10
If you need to maintain the decimal point then you can either :
Times everything by 10 which will give you one decimal place.
Times everything by 100 which will give you two decimal places.
Use the Floating point library available for Flowcode which will give you a 32 bit floating point number.
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
- 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:
Yes that should work fine.
You may have to be careful of the (inch * 254/10) part as I am unsure as to which will go first.
Eg the inch * 254 or the 254/10
for a complete example it is best to specify wich parts will go first eg.
cm = (((inch * 254) / 10) + 5 ) / 10
You may have to be careful of the (inch * 254/10) part as I am unsure as to which will go first.
Eg the inch * 254 or the 254/10
for a complete example it is best to specify wich parts will go first eg.
cm = (((inch * 254) / 10) + 5 ) / 10
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
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
I don't think you need to "+5" in this last example, because you are avoiding the rounding issue in the first place. So it may be better as:
But note that if you take this approach, your maximum value for "inch" is 129 because a larger value will exceed the maximum value for an "INT" variable in Flowcode (which is 32767).
Perhaps if your "inch" value is larger, you can do something similar to this:
But again, beware that there is a maximum allowable "inch" value (around 1310).
Even better might be:
This will allow the maximum range of "inch" values.
Code: Select all
cm = (inch * 254) / 100
Perhaps if your "inch" value is larger, you can do something similar to this:
Code: Select all
cm = ((inch*25)/10) + ((inch*4)/100)
Even better might be:
Code: Select all
cm = (inch*2) + ((inch*5)/10) + ((inch*4)/100)
Steve, don't understand why this is avoiding the rounding issue. In your cm = (inch * 254) / 100 if inch =1 the calculated value is 2.54 which presumably is truncated to 2. So don’t I still need the +5, or +50 in this case?
Also I tried to find β€32 bit floating point number’ in Flowcode help files and Forum and returned nothing useful. How are 32 bit floating point numbers accessed/used?
Also I tried to find β€32 bit floating point number’ in Flowcode help files and Forum and returned nothing useful. How are 32 bit floating point numbers accessed/used?
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
Yep - you are correct. It was too early in the morning for me!
If you want that rounding accuracy, use this:
But note that the maximum allowed inch value will be 129.
Details of the floating point library can be found here:
http://www.matrixmultimedia.com/support ... .php?t=505
If you want that rounding accuracy, use this:
Code: Select all
cm = (((inch * 254) / 10) + 5 ) / 10
Details of the floating point library can be found here:
http://www.matrixmultimedia.com/support ... .php?t=505