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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same calculation gives different answers.... 1

Status
Not open for further replies.

HorseGoose

Programmer
Apr 24, 2003
40
GB
I have some code below which calculates the strength of an acid indexed against citric. However, when the code runs it does not give the right answer even though the algoritims are correct. If the same calculations are put into a spread sheet they work.

here is the code and where the error happens

Public Function citriceq(varmw As Double, varpka1 As Double, varpka2 As Double, varpka3 As Double)
' This function converts any acid to a percentage relative to citric acid
' for example if tartaric acid is 75% as strong as citric you need to add target citric value/0.75 to get
' the correct value to add to a beverage which yields the beverage acidity as citric %w/w

Dim varpH, varr1d, varr2d, varr3d, varr4d, varf1d, varf2d, varf3d, varf4d, varfrac, varmM, varmg, varmgcitric83 As Double

varpH = 8.3 ' constant of unconverated alkalinity no pkas used
varmgcitric83 = 0.643039853 ' mg of citric acid at pH 8.3 to index against, this is effectively 100%

If varpka1 = 0 Then varpka1 = 20 ' if an acid has only pka values then third is converted to 20 so the algoritim can work
If varpka2 = 0 Then varpka2 = 20
If varpka3 = 0 Then varpka3 = 20

If varmw = 0 Then GoTo errorproc ' must have molecular weight

varr1d = 10 ^ (varpH - varpka1)
varr2d = 10 ^ (varpH - varpka2)
varr3d = 10 ^ (varpH - varpka3) 'ERROR HAPPENS HERE.....
varf1d = 1 / (1 + varr1d + varr1d * varr2d + varr1d * varr2d * varr3d)
varf2d = varr1d * varf1d
varr3d = varr1d * varr2d * varf1d
varr4d = varr1d * varr2d * varr3d * varf1d
varfrac = varf2d + 2 * varf3d + 3 * varf4d

varmM = 0.01 * varfrac

varmg = varmM * varmw

citriceq = varmgcitric83 / varmg
Exit Function

errorproc:
citriceq = "err"

End Function
========================================================
when the variables are as follows...

varmw = 98.00
varpka1 = 2.12
varpka2 = 7.2
varpka3 = 12.44

The out put of varr3d = 10 ^ (varpH - varpka3) should be 7.2236E-05 but in the function it is 0.96350

It could be that the program needs to finish one calcuation before starting another one but I have no idea how to make it do this. I am happy to sne dthe file to anyone who can help.

Thanks a lot
Horse Goose
 
Shouldn't be:
var[red]f[/red]3d = varr1d * varr2d * varf1d
var[red]f[/red]4d = varr1d * varr2d * varr3d * varf1d
?

combo
 
I only have two eyes and as many brain cells, you are correct. Sorry to ask such a dumb question but the numbers were spinning around and around there for a minute.

THANKS!!!!

HG
 
varr3d = varr1d * varr2d * varf1d makes the value 0.96350.

When I run it, the earlier value was 7.2236E-05, as required. (I used a couple of msgboxes to give the intermediate values.

Fen
 
Thanks for taking the time anyway, I am pleased it is solved but a bit embarrassed that it was just typos.

Cheers

HG
 
Probably not going to make a huge difference, but doesn't
Code:
Dim varpH, varr1d, varr2d, varr3d, varr4d, varf1d, varf2d, varf3d, varf4d, varfrac, varmM, varmg, varmgcitric83 As Double
define them all as variant except for varmgcitric83? If your inputs are doubles, shouldn't your return value be a double too? If this is the case, shouldn't the work fields be the same?

combo may have hit on the problem. I don't know the formula you are using, but operator precedence might give you a problem with
Code:
varf1d = 1 / (1 + varr1d + varr1d * varr2d + varr1d * varr2d * varr3d)
which will be interpreted as
Code:
varf1d = 1 / (1 + varr1d + [red]([/red]varr1d * varr2d[red])[/red] + [red]([/red]varr1d * varr2d * varr3d[red])[/red])
which might not be what you expect?



Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Also, you might consider using slightly more intuitive names for your variables (I might be wrong - in the context of acid comparison, they might be totally intuitive). When you come back to maintain this in six month's time, it will make your life easier...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
thanks steve, in fact they make sense to a chemist so thats OK.

I wasn't sure if you dimmed many variables on the same line and then gave a variable class at the end i thought all the variables in that line then took that class e.g.

dim a,b,c as string ' all dimmed as strings

dim a,b as string ' a and b are strings while c is a double.
dim c as double

is this wrong?
 
VBA Help said:
In the following statement, intX and intY are declared as type Variant; only intZ is declared as type Integer.

Dim intX, intY, intZ As Integer

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top