Rounding of values

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Rounding of values

Post by echase »

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

User avatar
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:

Post by Benj »

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.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Post by echase »

or presuambly cm = ((inch * 254/10) + 5 ) / 10

User avatar
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:

Post by Benj »

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

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

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:

Code: Select all

cm = (inch * 254) / 100
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:

Code: Select all

cm = ((inch*25)/10) + ((inch*4)/100)
But again, beware that there is a maximum allowable "inch" value (around 1310).

Even better might be:

Code: Select all

cm = (inch*2) + ((inch*5)/10) + ((inch*4)/100)
This will allow the maximum range of "inch" values.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Post by echase »

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?

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

Yep - you are correct. It was too early in the morning for me!

If you want that rounding accuracy, use this:

Code: Select all

cm = (((inch * 254) / 10) + 5 ) / 10
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

Post Reply