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

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Fuzz and doubles.
« Reply #15 on: May 11, 2007, 05:00:49 PM »
Beauty, I'll play with that on the weekend.
Thanks.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Fuzz and doubles.
« Reply #16 on: May 11, 2007, 07:28:57 PM »
Been a while since I've used c# but a static class variable/property is the most simple way to store a global 'like' value, for instance you could create a class called Utils which has a static property called fuzz which could be used like -

if(value >= Utils.fuzz()) //some comparison
{
    blah;
}

your property would look 'something' like

// the class property
static double fuzz()
{
    get
    {
        return myfuzz; //which is stored as a private variable in this class
    }
}

hth
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Fuzz and doubles.
« Reply #17 on: May 11, 2007, 07:59:17 PM »
Great, I'll give it a go this weekend.
5pm here, the amber liguid is calling something wicked.

sinc

  • Guest
Re: Fuzz and doubles.
« Reply #18 on: May 11, 2007, 08:10:16 PM »
I think MickD just gave the exact same answer I did.   :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Fuzz and doubles.
« Reply #19 on: May 11, 2007, 08:29:09 PM »
Can I say "it depends" ??

I'd be just as happy doing the test in-line as Sinc's Reply#12

... except use an exponent ... easy for my old brain; if the exponent is -8 thats 8 significant places, no need to count zero's .. and the test criteria is immediately transparent.

I'd like a buck for every time I've shot myself in the foot trying to be too smart.

This seems pretty transparent :-
Code: [Select]
    public class FuzzyTester
    {
        [CommandMethod("CmdFuzz")]
        static public void CmdFuzz()
        {
            double num1 = 10.00000001;
            //double num2 = 10.00000002;
            double num2 = 10.00000005;

            if (Math.Abs(num1 - num2) < 1.0e-8)
            {
                MessageBox.Show("Bingo !", "My Fuzzy Interpreter",
                     MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else           
            {
                MessageBox.Show("Pffffft !", "My Fuzzy Interpreter",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning); 
            }
        } 
    }



Bryco, Just a note on this ;

bool B=false;

You do not need to initialise the Value to false.
The process of declaring the variable
ie:
bool B;

will initialise the variable to false by default.

Similarly ;
int num;
will initialise num to 0 [zero]

Have a good weekend :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Fuzz and doubles.
« Reply #20 on: May 11, 2007, 08:40:15 PM »
AND .. just in case anyone watching is wondering why a fuzz factor is necessary sometimes ;

A Piccy !

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Fuzz and doubles.
« Reply #21 on: May 11, 2007, 08:50:07 PM »
I think MickD just gave the exact same answer I did.   :-)

Yes you're right, I think I did and my appologies, it was just my eyes glazed over scanning your post when you were looking into NOD's and such and I got lost :laugh:
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

LE

  • Guest
Re: Fuzz and doubles.
« Reply #22 on: May 11, 2007, 08:55:42 PM »
porting one of my c++ functions just to add more salsa to the tacos.
Code: [Select]
static bool eqDoubles(double i1, double i2, double ftol)
{
    double nom, denom, error;
    nom = Math.Abs(i1 - i2);
    denom = Math.Abs(i1) + Math.Abs(i2);
    if (denom != 0)
    {
        error = 2 * nom / denom;
        return (error <= ftol);
    }
    return true;
}

Code: [Select]
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;

Double num1 = 10.00000001;
Double num2 = 10.00000005;

if (eqDoubles(num1, num2, 1.0e-8))
    ed.WriteMessage("\n[They are Equal]");
else
    ed.WriteMessage("\n[They are Not Equal]");

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Fuzz and doubles.
« Reply #23 on: May 11, 2007, 09:00:15 PM »
I s'pose another debate is
should it be

if (Math.Abs(num1 - num2) < 1.0e-8)

or

if (Math.Abs(num1 - num2) <= 1.0e-8)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

sinc

  • Guest
Re: Fuzz and doubles.
« Reply #24 on: May 11, 2007, 09:01:36 PM »
I think MickD just gave the exact same answer I did.   :-)

Yes you're right, I think I did and my appologies, it was just my eyes glazed over scanning your post when you were looking into NOD's and such and I got lost :laugh:

Yeah, that might've muddled up the matter.  But it was an example that I happened to have on-hand.  And it's actually a particularly useful example - the NOD is the primary way to store application-specific data in the drawing with C#.  So if you want each drawing to remember the last command settings, even when you shut down Autocad and restart it, the NOD is the way to go.

Just an extra tidbit for Bryco to chew on this weekend, while I do some experimenting myself with ImpCurves...   :laugh:

sinc

  • Guest
Re: Fuzz and doubles.
« Reply #25 on: May 11, 2007, 09:03:19 PM »
I s'pose another debate is
should it be

if (Math.Abs(num1 - num2) < 1.0e-8)

or

if (Math.Abs(num1 - num2) <= 1.0e-8)


Is one any faster than the other?

Chuck Gabriel

  • Guest
Re: Fuzz and doubles.
« Reply #26 on: May 11, 2007, 09:03:24 PM »
Bryco, Just a note on this ;

bool B=false;

You do not need to initialise the Value to false.
The process of declaring the variable
ie:
bool B;

will initialise the variable to false by default.

Similarly ;
int num;
will initialise num to 0 [zero]

Have a good weekend :-)


I was going to ask if that behavior is documented, but then I thought "why not look it up for yourself?"  I did, and it is.  Somehow it still just feels wrong to me, but thanks for the tip.

sinc

  • Guest
Re: Fuzz and doubles.
« Reply #27 on: May 11, 2007, 09:04:50 PM »
I tend to do stuff like that explicitly anyway, for clarity of code.

The compiler makes it the same at runtime.

sinc

  • Guest
Re: Fuzz and doubles.
« Reply #28 on: May 11, 2007, 09:07:01 PM »
I tend to do stuff like that explicitly anyway, for clarity of code.

The compiler makes it the same at runtime.

Oh, wait, maybe it doesn't...

It would be the same for optimized C code, but I actually have no idea what happens in C#...

Chuck Gabriel

  • Guest
Re: Fuzz and doubles.
« Reply #29 on: May 11, 2007, 09:13:56 PM »
I tend to do stuff like that explicitly anyway, for clarity of code.

The compiler makes it the same at runtime.

Oh, wait, maybe it doesn't...

It would be the same for optimized C code, but I actually have no idea what happens in C#...

Maybe I'll play around with Reflector and see if there are any differences in the IL.