• C

# Two Maths functions on the same variable

Hi,

I have a variable called var1, it's value changes throughout the program.

Towards the bottom of the program I have some maths functions which use var1.

ru = (var1 / 4) * 100;
rh = (rsg  / var1) * 100

Now the trouble I have is that I need the var1 to be the same value in both equations, but at the moment the second var1 value is the first var1 value divided by 4.

Whats the best round this?  Do I copy the var1 variable to another variable before doing the sums, if so how would this be done ?

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
>>Now the trouble I have is that I need the var1 to be the same value in both equations, but at the moment the second var1 value is the first var1 value divided by 4.
If this was true then you probably either have a compiler bug or these are not sequential calculations.

If these are not sequential (you change var1 between them) then you DO indeed need to take a copy of var1 before it is changed as you suggest.

oldVar1 = var1;
ru = (var1 / 4) * 100;
...
rh = (rsg  / oldVar1) * 100

Paul
0

Experts Exchange Solution brought to you by