Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SMath Function 3

Status
Not open for further replies.

dik

Structural
Apr 13, 2001
26,044
Does anyone have a bit of code for a function to confirm that two numbers are approximately equal to each other (using a stipulated difference say 5%?) I can do it if both the numbers are +ve or both are -ve, but it fails at values near 0, where one is +ve and one is -ve. Any help appreciated.

Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
Replies continue below

Recommended for you

An Excel formula that works is:
=IF(A1-A2=0,1,ABS(A1-A2)/MAX(ABS(A1),ABS(A2))<Tol)
where Tol is a named range with the tolerance.

It should be pretty easy to convert that to SMath.

Doug Jenkins
Interactive Design Services
 
Thanks, Doug... will take a look at it.

Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
image_yaly8c.png




Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
can you give me a hint?

Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
MintJulep said:
That could be simplified a lot of you embraced Boolean.

I think as written is more readable, but if the function is going to be called from other routines it would certainly make things simpler to return a boolean True/False, or 1/0, rather than a text string.

One general question is whether checking the % difference compared to the larger value is always appropriate. Numbers of different sign will always differ by more than 100%, so for instance 1E-50 and -1E-50 will be found not equal.

Perhaps there should be an absolute value that will be treated as zero, or a value that will be treated as the reference if both values are smaller.

Doug Jenkins
Interactive Design Services
 

...not called from other routines, but as a notification that the two answers are 'correct' or comparable.

Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
Thanks, IRS... just what I wanted... I just found out that you can Windows paste directly into SMath...

image_u5xcin.png


Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
To Doug's point about values straddling zero, this is a much easier question to answer if tolerance is a number rather than a percentage.
 
MintJulep - OK, that's neater :)

Excel version:
=ABS(A1-A2) <= (Tol * MAX(ABS(A1),ABS(A2)))

I still think we need a check for two numbers very close to zero though.

Doug Jenkins
Interactive Design Services
 
Improved... I had modified the original in case the second arg was a zero... Fixt...

image_e2psxa.png


and in the program... the dimensions are all the same because I just 'cut and pasted' the variables and changed the name.

image_uvenm9.png


Rather than think climate change and the corona virus as science, think of it as the wrath of God. Feel any better?

-Dik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor