×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# SMath Function3

## SMath Function

(OP)
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

### RE: SMath Function

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
http://newtonexcelbach.wordpress.com/

### RE: SMath Function

(OP)
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

### RE: SMath Function

(OP)

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

-Dik

### RE: SMath Function

That could be simplified a lot of you embraced Boolean.

### RE: SMath Function

(OP)
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

### RE: SMath Function

#### Quote (MintJulep)

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
http://newtonexcelbach.wordpress.com/

### RE: SMath Function

(OP)

#### Quote (if the function is going to be called from other routines)

...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

### RE: SMath Function

MintJulep - but what if a and b are both zero?

Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/

### RE: SMath Function

(OP)
Thanks, IRS... just what I wanted... I just found out that you can Windows paste directly into SMath...

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

-Dik

### RE: SMath Function

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.

### RE: SMath Function

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
http://newtonexcelbach.wordpress.com/

### RE: SMath Function

(OP)
Improved... I had modified the original in case the second arg was a zero... Fixt...

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

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

-Dik

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!