Code Comments
Programming Forum and web based access to our favorite programming groups.Let's look at how changing logic is used in a classic RW response -- Bill Klein wmklein <at> ix.netcom.com "Robert Wagner" <robert@wagner.net.yourmammaharvests> wrote in message news:052aj0lmkgdi0r22g75jskjrqg8vv4se50@ 4ax.com... > On 30 Aug 2004 12:39:58 -0700, riplin@Azonic.co.nz (Richard) wrote: <snip> > What if the numbers were not truncated, computed or rounded? I posted > a demo of that case to which you had no reasonable answer. > > > You're incorrect. It doesn't work that way in C# nor in JavaScript (I > don't know about compiled Java). > <snip> The original post stated, "works identically in primary school textbooks, mechanical adding machines, C, Java, and Cobol." The response says that this is INCORRECT because (and I quote) It doesn't work that way in C# nor in JavaScript (I don't know about compil ed Java)." So now, tell me why does RW think this response has any relationship to the original statement? If the original statement had been, "works identically in all variations and derrivites of Java and C and COBOL. " then his response would have been responsive. Only (well that IS an exageration <G> ) RW would respond to "A and B work the same" with the statement "Incorrect, A and D don't work the same" *** Again, (because I think I know what he MEANT to say), it would be nice if he would respond correctly with a statement like, "Sorry for my original mis-statement. What I meant to say was You may (or may not) be right when you said ' rounding mechanism is well defined and works identically in primary school textbooks, mechanical adding machines, >>C, Java, and Cobol.' but it is also true that there are many other programming languages that use other approaches for rounding that avoid what I think is an unwarrented bias . For example, C# and JavaScript use "bankers' rounding" see: For C# : http://msdn.microsoft.com/library/d...ndtopic3.aspFor java script: well, oops, according to at l east one reference, JavaScript does NOT use"bankers rounding" for its "norma l" rounding. See:ht tp://www.developingskills.com/ds.php?article=jsround&page=1which says,"Math. round - if the decimal part is 0.5 or more, it is rounded up. If it's lessth an 0.5, it is rounded down. This is just the way we're taught at school and isideal unless we specifica lly need something different."and later says,"Many programming languages use a type of rounding called "round to even" or"banker's rounding". This means that rounding works in the normal way EXCEPTwhen the decimal part is exactl y 0.5. In this case it will round to the nearesteven number. So 1.5 rounds to 2 and so does 2.5. The Jav ascript function toachieve this is: function roundToEven(num){ if ((Math.floor(num)%2==0) && (Math.abs(num-Math.floor(num))==0.5)) return Math.roun d(num)-1; else return Math.round(num); }"
Post Follow-up to this messageOops formatting error at the end of original message. Formatting now (I hop e) corrected -- Bill Klein wmklein <at> ix.netcom.com "William M. Klein" <wmklein@nospam.netcom.com> wrote in message news:qaudnWZHTdNPoKjcRVn-vg@comcast.com... > Let's look at how changing logic is used in a classic RW response > > -- > Bill Klein > wmklein <at> ix.netcom.com > "Robert Wagner" <robert@wagner.net.yourmammaharvests> wrote in message > news:052aj0lmkgdi0r22g75jskjrqg8vv4se50@ 4ax.com... > <snip> > > <snip> > > The original post stated, > > "works identically in primary school textbooks, mechanical adding machines , C, > Java, and Cobol." > > The response says that this is INCORRECT because (and I quote) > > It doesn't work that way in C# nor in JavaScript (I don't know about comp iled > Java)." > > So now, tell me why does RW think this response has any relationship to th e > original statement? > > If the original statement had been, > > "works identically in all variations and derrivites of Java and C and COBO L." > > then his response would have been responsive. > > Only (well that IS an exageration <G> ) RW would respond to > > "A and B work the same" > with the statement > "Incorrect, A and D don't work the same" > > *** > > Again, (because I think I know what he MEANT to say), it would be nice if he > would respond correctly with a statement like, > > "Sorry for my original mis-statement. What I meant to say was > > You may (or may not) be right when you said ' rounding mechanism is well > defined and works identically in primary school textbooks, mechanical addi ng > machines, >>C, Java, and Cobol.' > > but it is also true that there are many other programming languages that u se > other approaches for rounding that avoid what I think is an unwarrented bi as. > For example, C# and JavaScript use "bankers' rounding" see: > > For C# : > http://msdn.microsoft.com/library/d...roundtopic3.asp For java script: well, oops, according to at least one reference, JavaScript does NOT use"bankers rounding" for its "normal" rounding. See: http://www.developingskills.com/ds....=jsround&page=1 which says, "Math.round - if the decimal part is 0.5 or more, it is rounded up. If it's lessthan 0.5, it is rounded down. This is just the way we're taught at schoo l and isideal unless we specifically need something different." and later says, "Many programming languages use a type of rounding called "round to even" or"banker's rounding". This means that rounding works in the normal way EXCEPTwhen the decimal part is exactly 0.5. In this case it will round to th e nearesteven number. So 1.5 rounds to 2 and so does 2.5. The Javascript function to achieve this is: function roundToEven(num){ if ((Math.floor(num)%2==0) && (Math.abs(num-Math.floor(num))==0.5)) return Math.round(num)-1; else return Math.round(num); } There are plenty more types of rounding, including random rounding, alternat e rounding, symmetric rounding and asymmetric rounding. Microsoft have written an interesting HOWTO covering these. Unfortunately it's written with Visual Bas ic developers in mind, but there's plenty to think about no matter what languag e you write your software in."
Post Follow-up to this messageOn Tue, 31 Aug 2004 22:11:12 -0500, "William M. Klein" <wmklein@nospam.netcom.com> wrote: >Let's look at how changing logic is used in a classic RW response >"Robert Wagner" <robert@wagner.net.yourmammaharvests> wrote in message > news:052aj0lmkgdi0r22g75jskjrqg8vv4se50@ 4ax.com... >So now, tell me why does RW think this response has any relationship to the >original statement? Because C# is an extension of C++ and JavaScript is a dialect of Java. For C#: > http://msdn.microsoft.com/library/d...roundtopic3.asp For java script: well, oops, according to at least one reference, JavaScript does NOT use"bankers rounding" for its "normal" rounding. The Web site you posted above shows JScript under .NET doing Bankers' Rounding. We're all programmers here. Rather than read about it, let's run it to see what happens. <html> <body> <p><center> <script language="Javascript"> document.write(Math.round(2.5)) </script> </body> </html> Iit displays a 3. You were right. I thought it rounded to even because this site says so. It also says "the notion that Javascript will run exactly the same on any two machines is a fantasy." http://www.209software.com/books/wpid/sect3/ch01.shtml
Post Follow-up to this messageOn Tue, 31 Aug 2004 22:11:12 -0500, "William M. Klein" <wmklein@nospam.netcom.com> wrote: >Let's look at how changing logic is used in a classic RW response >"Robert Wagner" <robert@wagner.net.yourmammaharvests> wrote in message > news:052aj0lmkgdi0r22g75jskjrqg8vv4se50@ 4ax.com... >So now, tell me why does RW think this response has any relationship to the >original statement? Because C# is an extension of C++ and JavaScript is a dialect of Java. For C#: > http://msdn.microsoft.com/library/d...roundtopic3.asp For java script: well, oops, according to at least one reference, JavaScript does NOT use"bankers rounding" for its "normal" rounding. The Web site you posted above shows JScript under .NET doing Bankers' Rounding. We're all programmers here. Rather than read about it, let's run it to see what happens. <html> <body> <p><center> <script language="Javascript"> document.write(Math.round(2.5)) </script> </body> </html> Iit displays a 3. You were right. I thought it rounded to even because this site says so. It also says "the notion that Javascript will run exactly the same on any two machines is a fantasy." http://www.209software.com/books/wpid/sect3/ch01.shtml
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.