Android Question Convert NaN to Zero (SOLVED)


New Member
Licensed User
Longtime User
Hi everyone,

Forgive me if this question has been asked before or direct me to the thread. I'm working on a bit of code that has to deal with DIV/0 (see below). In VB I used "On Error Resume Next" but here and, the DIV/0 actually returns NaN which doesn't act like 0 when used in mathematical formulas.

What I need to know is how to convert NaN as a result of dividing by 0 to make it equal 0. Here's my example. If I use fuel model 1 in the equation, I need f_i2 to equal 0 not NaN because's it's used later to get F which is used later to add to something which is used later to create another variable, etc. This is a scaled down example of a formula that's over 300 lines long and there's 40 fuel models to chose from and they are supposedly to coming out with more next year.

In other words, How can I get a value for F even if a,b,c,d,e happens to be 0. Each model has a least one 0 for a variable. It's unavoidable. Any help would be greatly appreciated. Thanks in advance.

Fuel Model 1
a= 2.9
b= 0.41
c= 0.15
d= 0
e= 0

Fuel Model 5
a= 3
b= 0
c= 0
d= 2.6
e= 1.3

OnehrSurfacefVolRatio = 2500
TenhrSurfacefVolRatio = 109
HundhrSurfacefVolRatio = 30
LvHerbSurfaceVolRatio = 1500
LvWoodySurfaceVolRatio = 1500
OnehrFuelLoad = a
TenhrFuelLoad = b
HundhrFuelLoad = c
LvHerbFuelLoad = d
LvWoodyFuelLoad = e

A_i1j1 = ((OnehrSurfacefVolRatio * OnehrFuelLoad) / 32)
A_i1j2 = (TenhrSurfacefVolRatio * TenhrFuelLoad) / 32
A_i1j3 = (HundhrSurfacefVolRatio * HundhrFuelLoad) / 32
A_i2j1 = (LvHerbSurfaceVolRatio * LvHerbFuelLoad) / 32
A_i2j2 = (LvWoodySurfaceVolRatio * LvWoodyFuelLoad) / 32
A_i1 = A_i1j1 + A_i1j2 + A_i1j3
A_i2 = A_i2j1 + A_i2j2
A_T = A_i1 + A_i2
f_i1j1 = A_i1j1 / A_i1
f_i1j2 = A_i1j2 / A_i1
f_i1j3 = A_i1j3 / A_i1
f_i2j1 = A_i2j1 / A_i2
f_i2j2 = A_i2j2 / A_i2
f_i1 = f_i1j1 + f_i1j2 + f_i1j3
f_i2 = f_i2j1 + f_i2j2

F= (500 * f_i1) + (800 * f_i2)

Msgbox (F,"Value for F")



New Member
Licensed User
Longtime User
Ok. That's what I've started working on. I was hoping for an simpler solution. This code comes from an old Excel Vba program written which I handled before using the on Error Resume Next because it would throw an exception and when added to another value it was just a empty string which worked to my advantage.

Thanks for the input. I appreciate it.
Upvote 0


Licensed User
Longtime User
You can write a sub for it so you dont need hundrets of IF THENs

'if A_i1 = 0 then f_i1j1 = 0 else f_i1j1 = A_i1j1 / A_i1
f_i1j1 = calc(A_i1,A_i1j1)
[..more calculations here..]

sub calc(one as double , two as double) as double
  if one = 0 then
    return 0
    return two / one
  end if
end sub

Please note: i wrote it here in forums editor. not in b4a... so i´m not sure if it works :)
Upvote 0


New Member
Licensed User
Longtime User
It makes sense. Thanks everyone for the input. You've help tremendously. I'm new to the forum. Do I mark this solved somewhere?
Upvote 0