Solved

ksh; trap math errors

Posted on 2006-07-17
7
1,070 Views
Last Modified: 2013-12-26
How do you protect math operations like

(( result = num1 * num2 + num3 / num4 ))

from killing the program.   I can check all the variables for null values, numbers
only and zero, when important, but is there a way to skip all the checking and just trap it?

I can check for an legit result value later.

Thanks

ksh
0
Comment
Question by:hank1
7 Comments
 
LVL 5

Expert Comment

by:bastibartel
ID: 17121628
Hi hank1,
try
{
    double nom = 1.0;
    double den = 0.0;
    double res = nom/den;
}
catch (...)
{
    fprintf(stderr;"Some unknow exception occured\n");
}

Cheers!
Sebastian
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17121723
> but is there a way to skip all the checking and just trap it?
don't use a shell for math calculations, ther tons of better tools for that
0
 
LVL 1

Author Comment

by:hank1
ID: 17121794
try catch?.. in ksh?
Ok, I'd rather use perl for this stuff but this is a ksh script and that's that.  :-)
I sensing that this is a check all the vars thing.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:hank1
ID: 17121896
I came up with

result=`echo "expression" | bc`

then check the result.  Divide by zero contains the string 'divide'.  Lame,
but beats the lines and lines of checking.

 I'll delete the question.
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17123747
:-)  sry - didn't see the reference to ksh.
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17157387
Closed, 125 points refunded.
Netminder
Site Admin
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
paragon account problem 9 96
Perl Awk Need Help 3 95
Named range not carried over 10 57
How to determine file rename from EventLog ? 2 36
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now