lack of i++ and float?

masterZ

New member
First of all, how do float nubmers work.
For example, when i type

float number = 1/2;
print( number );

gives
0.0

also, why is there a lack of a variable++ and variable-- function?

thank you
 

Bale

Minion
The problem is that when kolmafia divides an integer by an integer it will produce an integer as a result. It automatically drops everything beyond the decimal point. You'll get an entirely different result if you do this:

float number = 1/2.0;
print(number);

Since 2.0 is float, it will give a float as a result. (I figured that out the hard way by tracking down a round-off bug in a very long script.)

There is no variable++ or variable-- because it is so easy to use:

counter = counter +1;
counter = counter -1;

Edit: I was ninja'ed, but claim superiority through verbosity.
 

Veracity

Developer
Staff member
float number = 1/2;
As has been pointed out, when KoLmafia parses the "1/2" expression, it sees two integers and ends up with an integer.

It could be argued that since we know the expression is going to be assigned to a float, the whole expression should be evaluated as floats. Currently, each operator only knows the types of its operands, not the larger context.

On the other hand, what would you expect the following to produce?

Code:
int num = 123;
float fnum = ( num / 7 ) * 7 + ( num % 7 );
print( fnum );

Currently, with nothing but integers on the right side, you get 123.0.
If we did the right side as all floats, you'd get 127.0.

I'm pretty sure that I would expect 123.0...
 
Last edited:

Alhifar

Member
I would expect it to produce 127 (Which is what mafia produces if you use 7.0 instead of 7). ( 123/7 ) * 7 should produce 123. ( 123 % 7 ) should produce 4. Those definitely add up to 127.
 

Veracity

Developer
Staff member
Your expectations would not be met by most programming languages. Consider Java:

Code:
public abstract class Test
{
	public static final void main( final String[] args )
	{
                int num = 123;
                float fnum = ( num / 7 ) * 7 + ( num % 7 );
                System.out.println( fnum );
	}
}

Executing it produces the following:

$ java -jar test.jar
123.0
 
Top