Archived

This topic is now archived and is closed to further replies.

xyz

c++ to c# prob....

Recommended Posts

Hi, I have this in c++: float len = float(sqrt(x*y)); if(len) { ... } how can do this if statement in c#? Thanks

Share this post


Link to post
Share on other sites
Having a float as your conditional for an if is not good design, since rounding errors can have even a supposedly 0 value equal to a very small number instead. You should use a threshold if you want to compare a float value to zero
ex.

#define THRESHOLD 0.00001
float var = (float) (sqrt(x));
if(var < THRESHOLD){
//blah blah blah
}

Share this post


Link to post
Share on other sites
No need to define a threshold since .Net already has one.

float len = (float)Math.Sqrt(x*y);
if ((len > - float.Epsilon)&&(len < float.Epsilon))
{
//...Equal to zero
}

[edited by - eagleone on March 18, 2004 2:47:45 PM]

Share this post


Link to post
Share on other sites