Solved

ksh; trap math errors

Posted on 2006-07-17
7
1,080 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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