Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Divided/0

Status
Not open for further replies.

alisaif

ISP
Apr 6, 2013
418
AE
Hi,

Can we avoid Numeric Overflow.Data was lost.

Saif
 
As titled "Divided/0" is popular error. basic/simple solution

xQty=0
xVal=100
xrat=IIF(xQty#0,xVal/xQty,0)
? xrat

You might know it, In which situation you need to avoid this error.
------------------
Another reason as per help file "A mathematical operation resulted in a number that was too large to be stored in the field or variable in which it was placed."
 
That is what I would do, anytime you need to divide, check the divisor/denominator,
I tend to do it more obviously (to me)
Code:
xQty=0
xVal=100
xRat=0
if xQty <> 0
  xRat=xVal/xQty
endif
[code]
but the net result is the same

Regards

Griff
Keep [Smile]ing

[center]There are 10 kinds of people in the world, those who understand binary and those who don't.

[i]I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are not good for you. [/i][/center]
 
Another method when extreme precision is not required would be to add/subtract a tiny number to/from the divisor.

[tt]xQty = 0 + .000000000001[/tt]
[tt]xVal = 100[/tt]
[tt]xrat = xVal/xQty[/tt]
[tt]? xrat[/tt]


mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
That is clever. I like that

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are not good for you.
 
If you want to throw an error when dividing by 0, you can use a trick, just adding this single line before the real division:

=MOD(XX,YY) && If YY=0, "Cannot divide by 0" error is thrown
*-- Real division
aa = XX/YY

Regards.-
 
Dividing by 0 is not the only way to leave the numeric precision or field length of an N field.
The general direction of solving by detecting 0 is ok, in the end you have to test whether STR(expression) contains "*"

The error 39 also happens when appending something not fitting the target table. In that case you may resize the table.

Bye, Olaf.
 
Hi,
The code below is mathematical nonsense - x/0 is NOT 0 it is indefinite!

xQty=0
xVal=100
xrat=IIF(xQty#0,xVal/xQty,0)
? xrat

Either use the Mod() test or something like

Code:
xQty=0
xVal=100
IF(xQty#0)
xrat = xVal/xQty
Else
*** some message to the user/developer
Endif 
? xrat

hth
MK
 
MK,
nevertheless in many cases a default value makes sense.

Say you compute a percentage of a component of a recipe. Say for whatever reasons there are no records of a recipe, so your total quantity is 0, your component quantity is 0 and when you try to calculate 0/0 you get the division error. It makes total sense to talk about an indefinite percentage of nothing in nothing, but for practical reasons you can still go on with 0% in that case.

In the end the handling of this kind of calculation has to be decided case by case. It makes sense to have a general approach much like having a general ON ERROR handler, but it also makes sense to cope with too few information and still be able to go on like using a TRY CATCH at specific places to handle some cases.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top