Author Topic: Fuzz and doubles.  (Read 9388 times)

0 Members and 1 Guest are viewing this topic.

TonyT

  • Guest
Re: Fuzz and doubles.
« Reply #45 on: May 13, 2007, 08:15:01 PM »
Hehe, I'm glad that it's not just me.
Kerry the vbaide forces the capital I in If,

actually I had just copied
Code: [Select]
Function Rd(num1 As Variant, num2 As Variant) As Boolean
    Dim dRet As Double
    dRet = num1 - num2
    If Abs(dRet) < 0.00000001 Then Rd = True
End Function
to C# thinking that nothing could be easier.
(Morphs into Hatch for a minute)
Oops no boolean in help, ok got it bool.
Abs, no no help on that. Ah add using system.Math.Abs
Oh, better not call this class Math then, changes it to MyMath.
public static bool Rd(double num1,double num2), why is the )lighting up? static public bool Rd, nope still red (Wonders if it the order makes any difference)
static bool Rd, still red static void. Oh need to put the bracket thing on the next line. (It lights up the ")" as it's wrong in the future, as in the next line)
Code: [Select]
    public static bool Rd(double num1,double num2)
    {
        double dRet = num1 - num2;
        bool B=false;
        If (math.Abs(dRet) < 0.00000001)
        {
          B= true;
        }
        else
        {
            B= false;
        }
        return B;
    }
It's red on the if line, staring at for 5 mins with hatred does little to fix it,finally If.
This is when I posted to make sure I even need this function.
I've still got to figure out the top line
double num1,double num2 needs to be maybe object num1,object num2? as it is not always a double being fed to the function.
Right now $250 for the standard edition sounds like a cheap investment to take some of the agro out of learning this program.


Quote
I've still got to figure out the top line double num1,double num2 needs to be maybe
object num1,object num2? as it is not always a double being fed to the function.

Well, I think one thing you still haven't figured out, is the fact
that there's significant differences beween VB6/VBA and C#
and VB.NET.  The advice I generally give to someone coming
from VB6/VBA is to try to forget much of what they know
about those languages, and start over.

An example of where you're making the mistake of trying to apply
VB6/VBA concepts to C# programming, is to type parameters as
'object' to allow one of several different types to be passed.

In OOP langauges that support overloading, we don't do that.

Instead, we overload a function/method to accept what types
we want to allow it to accept.

So, what is 'overloading' ?

Code: [Select]

   public static class MyComparer
   {
      public static bool Equals( int x, int y )
      {
         return x == y;
      }

      public static bool Equals( double x, double y )
      {
         return Math.Abs( x - y ) < fuzz;
      }

      public static bool Equals( float x, float y )
      {
         return Math.Abs( x - y ) < fuzz;
      }

      public static bool Equals( Decimal x, Decimal y )
      {
         return Math.Abs( x - y ) < fuzz;
      }

      public static double fuzz = Double.Epsilon;

   }


Notice that the above class has four methods, all of
which have the same name. So, how can that be?
If you can declare more than one method with the
same name, how does the compiler know which you
mean?  The answer is simple, each of the methods
have the same name and number of arguments, but
each accepts different types as their arguments.

The compiler knows which method to actually use
based on what types you pass in a call to it. So, if
you pass the Equals() method two doubles, it will
call the version that takes two doubles as params.

Overloading is used in place of using 'object' as the
type, because you do not have to resolve the
type at runtime (e.g., test it to see if it is one of
the types you can accept, and reject it otherwise).
Using 'object' as a parameter prevents compile-time
type checking/enforcement, which means that you
must instead do that at runtime, which on a large
scale, slows down code immensely. Just ask any
LISP programmer :-D

You may also be wondering why the bodies of three
of those functions are identical. Well, in fact they're
not, and that's because the Math.Abs() function is
also overloaded, and each of the 3 functions above
calls a different overloaded version of Abs().

When you type 'Math.Abs(' in the IDE, notice that
the intellisense tooltip kicks in and and displays
'1 of 7' with up/down arrows. If you use the down-
arrow key you can scroll through the overloaded
versions of the function, and see the parameters
they take.

Another comment is that you're not really writing
proper code when you do something like this:

Code: [Select]
   

    public static bool Rd(double num1,double num2)
    {
        double dRet = num1 - num2;
        bool B=false;
        If (math.Abs(dRet) < 0.00000001)
        {
          B= true;
        }
        else
        {
            B= false;
        }
        return B;
    }


The entire function is pointless because it is
no different than:

   public bool rd(double a, double b)
   {
       return Math.Abs(a - b) < 0.00000001;
   }

No variable to hold the result. No if/then/else.

And, as others have noted, the need to have
a function that encapsulates just a few very
simple operations, is questionable.

So I think your use of the variable 'B' seems
to suggest to me that you're still thinking about
code in terms of VB6/VBA, where you must
assign the result of a function to its name.

An even more basic misunderstanding is that
you do not use if/then/else to translate a
boolean to a boolean.

In other words,

Code: [Select]

    bool val;
   
    if( test )
        val = true;
    else
        val = false;


Because, that is no different than:

Code: [Select]

    bool val = test;


In C#, you can just use 'return <value>' to return
a value from a function. There is no need to assign
the result of the function to a variable.

Lastly, in C# or VB.NET, there is no such thing
as a 'global' variable. There are only types and
members (static and non-static). In the example
class above, there is a static variable called 'fuzz'
which can be used like a global variable, by simply
prepending the class name to it, like this:

Code: [Select]

     MyComparer.fuzz = 1.0e-10;


You use static members of classes the same
way you use global variables in VB6/VBA. I
used a simple variable here rather than a
property, so as to not drag the concept of
encapsulation into the mess, which would
only serve to confuse that with the more
basic concept of scope, but in real world
development, the above variable might be
a property with a setter/getter.

Bryco

  • Water Moccasin
  • Posts: 1850
Re: Fuzz and doubles.
« Reply #46 on: May 13, 2007, 11:10:28 PM »
That, Tony, is a really well written piece.
sinc mentioned overloading and I started looking at it but you've made it easy to figure out.
I have been trying the object way and so far haven't been successful.
Besides being a bad way to do it, it also seems a bit dodgy in that you may be getting a copy. Boxing is interesting but not easy to actually use.
The overloading is the GO.

TonyT

  • Guest
Re: Fuzz and doubles.
« Reply #47 on: May 13, 2007, 11:44:15 PM »
Sorry, Don't quite understand what you
mean by 'you may be getting a copy'.


That, Tony, is a really well written piece.
sinc mentioned overloading and I started looking at it but you've made it easy to figure out.
I have been trying the object way and so far haven't been successful.
Besides being a bad way to do it, it also seems a bit dodgy in that you may be getting a copy. Boxing is interesting but not easy to actually use.
The overloading is the GO.

Bryco

  • Water Moccasin
  • Posts: 1850
Re: Fuzz and doubles.
« Reply #48 on: May 14, 2007, 12:22:27 AM »
Nor do I. Too much reading in one weekend.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Fuzz and doubles.
« Reply #49 on: May 14, 2007, 06:29:10 PM »
...............
Well, I think one thing you still haven't figured out, is the fact
that there's significant differences beween VB6/VBA and C#
and VB.NET.  The advice I generally give to someone coming
from VB6/VBA is to try to forget much of what they know
about those languages, and start over.

...................

Excellent and helpful post.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--