Round Error? lazarus / 13-General ---------------------------------------------------------------------- DimProfi · 2007-01-31 11:42 Code: [Select]showmessage(inttostr(round(2.48))); shows -> "2" Why not "3" ? Using my mathematical knowledge gives me: 2,48 ->2,5 -> 3 ---------------------------------------------------------------------- felipemdc · 2007-01-31 12:35 > Using my mathematical knowledge gives me: 2,48 ->2,5 -> 3 What you proposed is considered rounding error, and happens after you execute Round operation sucessive times. You executed Round 2 times, first rounding only to 1 decimal, and finally rounding the a integer value. The correct result for Round(2.48) is 2 because it rounds in only 1 step. And that is also the correct mathematical value of it. ---------------------------------------------------------------------- eden159 · 2007-02-01 21:02 Quote from: "sekel"> Using my mathematical knowledge gives me: 2,48 ->2,5 -> 3 What you proposed is considered rounding error, and happens after you execute Round operation sucessive times. You executed Round 2 times, first rounding only to 1 decimal, and finally rounding the a integer value. The correct result for Round(2.48) is 2 because it rounds in only 1 step. And that is also the correct mathematical value of it. Can you be more specific how does he uses round() 2 times? As far as I can see it's only used 1 time. That's how I interpret the code (correct me if I'm wrong): First it's executed the Round() function with the value 2.48 - that should produce the value 3. Then this integer value is converted to string and is shown to the user. So can you explain step by step why the value should be 2, not 3? ---------------------------------------------------------------------- felipemdc · 2007-02-01 21:19 Quote from: "eden159"So can you explain step by step why the value should be 2, not 3? It´s by definition .... read here: http://en.wikipedia.org/wiki/Rounding The rounding method consists in:     * Decide which is the last digit to keep.     * Increase it by 1 if the next digit is 5 or more (this is called rounding up)     * Leave it the same if the next digit is 4 or less (this is called rounding down) So, Let´s Round 2.48 Our Pascal Round function will only keep the 2 digit. What is the digit right next to 2 ? It´s 4 So? Round down Round(2.48) = 2 ---------------------------------------------------------------------- eric · 2007-02-02 08:12 What sekel says is quite right. And you can also look at it in a different way: 2.48 is obviously less than half-way between 2 and 3, so it gets rounded down. 2.5 is exactly half-way, and by convention it gets rounded up. Eric ---------------------------------------------------------------------- Marc · 2007-02-02 12:40 How 2.5 is rounded is depending on the type of round. If you're use banker rounding then it would be 2 (always rounded to even) ---------------------------------------------------------------------- eric · 2007-02-02 19:17 Yes, but the normal common usage is to round to the nearest value up or down. Eric ---------------------------------------------------------------------- Danail · 2007-02-02 19:48 However it is not very dificult to be written new function that to round the digits on dimprofi's way. The free steps that sekel wrote may be used: * Decide which is the last digit to keep. * Increase it by 1 if the next digit is 5 or more (this is called rounding up) * Leave it the same if the next digit is 4 or less (this is called rounding down) an algorythm for that could look like that: 1)use the floattostr function to turn the digit into text    i.e.    2.48 will become the text '2.48'    take the last leter from the text using    (strlength(the_string) - 1) or    (strlength - somecounter) to extract the next last digit i.e. 8    take also the next second digit using    (strlength(the_string) -(somecounter-1))    use the StrToInt function to retrieve the last digit and the digit before it    as a digit i.e. 8 and 4. 2)Use the If operand to calculate +1 if needed. 3)Use IntToStr to store the resuld digit as a leter in a newstring. 4)If the next leter is '.' simply add it to the newstring. 5)Mirror the string. 6)Use StrToFloat to receive the result. If someone has time can do the task ---------------------------------------------------------------------- eric · 2007-02-03 08:06 Was that a little tongue-in-cheek, Danail? Eric ---------------------------------------------------------------------- Danail · 2007-02-03 14:57 This is the way I made similar thing but my task was to mirror a digit for example 12.56 to become 65.21 to add some another digit to the mirrored and to make it appear in a TLabel component