round off script does not function

Started by spook1, Mon 08/09/2003 09:42:43

Previous topic - Next topic

spook1

Again a round off question:

I calculate a voltage in microvolts. i want to reflect the value in volts.

the script I use is:

VoltageConnectedDigit1 = VoltageConnected / 1000000;
VoltageConnectedDigit2 = ( ( VoltageConnected - VoltageConnectedDigit1 * 1000000)/ 100000  ) ;

RoundOffRest = (VoltageConnected - (1000000 * VoltageConnectedDigit1) - (100000 * VoltageConnectedDigit2) );

if (RoundOffRest > 49999) VoltageConnectedDigit2 ++ ;// +1 : is to compensate for negative round off

RawPrint (25,52,"V= %d,%d", VoltageConnectedDigit1, VoltageConnectedDigit2);  // print Output Voltage to Voltmeter

My problem is that the roundoff rest is sometimes even 1628600 !!
It is orders of magnitude too big for what I expect???

Any suggestions?
kind regards,

martijn

Gilbert

I don't know, the code seems okay, but is it possible that VoltageConnected would exceed the limit of int (2,147,483,647)  or even a negative number?

SSH

Looks OK to me, too, but made me think: does AGS scripts have exacly the same precedence and association rules as C/C++? i.e.

is a - b - c  equivalent to (a-b) - c   or   a - (b - c)  ?

Maybe you could try (to be safe):

VoltageConnectedDigit2 = ( ( VoltageConnected - (VoltageConnectedDigit1 * 1000000))/ 100000 ) ;

RoundOffRest = ((VoltageConnected - (1000000 * VoltageConnectedDigit1)) - (100000 * VoltageConnectedDigit2) );
12

spook1

Quote from: Gilbot V7000a on Mon 08/09/2003 11:05:54
I don't know, the code seems okay, but is it possible that VoltageConnected would exceed the limit of int (2,147,483,647)  or even a negative number?

Nope, I thought of that, but I start with a voltage of 900000 mV, or sometimes 9,000.000,000 That's the max.

Thanx for the reply

spook1

Thanks, this did the trick. I had soemthing wrong with the brackets indeed.

Kind regards,

martijn

Gilbert

hehe but yet the max 9,000,000,000 does exceed the limit of the int type variable. ;)

SMF spam blocked by CleanTalk