This site requires JavaScript, please enable it in your browser!
Greenfoot back
DrCrane
DrCrane wrote ...

2018/1/29

Doubles to Integers

DrCrane DrCrane

2018/1/29

#
Hello, I've been having difficulty the last few days with a small bit of code that I can't figure out how to get working. Basically, I have an int called status_player_dmgoutput that I use as percentage (see code). I want to use that to multiply the result (for example 0.5 if its 50) with the int hp_change to get the change I need to add to the last recorded hp. What I currently am using is not working and old posts are not helping.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
     * damageEnemy - Remove (int value) amount of HP from the enemy.
     * Entering a value that causes the enemy to hit 0 or less will cause a win
     */
    public void damageEnemy(int hp_change)
    {
        //Setworld for code
        L2Battle l2battle = (L2Battle)getWorld();
        World world = getWorld();
         
        double hp_change_ws = hp_change * (status_player_dmgoutput/100);
         
        int hp_change_ws1 = (int) hp_change_ws;
       
        enemyHP = enemyHP - hp_change_ws1;
 
        if(enemyHP < 0)
        {
            enemyHP = 0;
        }
 
        //Execute change
        Greenfoot.delay(5);
        List<Actor> actors = (List) world.getObjects(RPG_HealthSystem_Enemy.class);
        List<Actor> actors2 = (List) world.getObjects(BarInfo_Enemy.class);
        world.removeObjects(actors);
        world.removeObjects(actors2);
        world.addObject(new RPG_HealthSystem_Enemy(enemyHP), 93, 107);
        world.addObject(new BarInfo_Enemy(enemyHP, enemyHP_MAX, enemyLVL), 132, 133);
 
        if(enemyHP == 0)
        {
            gameWin();
        }
    }
The part that is not working is
1
2
3
4
5
double hp_change_ws = hp_change * (status_player_dmgoutput/100);
         
        int hp_change_ws1 = (int) hp_change_ws;
       
        enemyHP = enemyHP - hp_change_ws1;
DrCrane DrCrane

2018/1/29

#
For debugging purposes you can assume
1
status_player_dmgoutput = 50
and
1
hp_change = 14
danpost danpost

2018/1/29

#
Try removing the parenthesis around 'status_player_dmgoutput/100'. You are probably doing an integer operation, causing undo loss of precision. By multiplying first, it becomes a double operation, basically eliminating any loss..
Vercility Vercility

2018/1/29

#
^this Use 100.0 for double calculations instead
danpost danpost

2018/1/29

#
Also, you may need to cast 'hp_change' as a double or the compiler may think it is an integer operation anyway.
DrCrane DrCrane

2018/1/29

#
danpost wrote...
Try removing the parenthesis around 'status_player_dmgoutput/100'. You are probably doing an integer operation, causing undo loss of precision. By multiplying first, it becomes a double operation, basically eliminating any loss..
That works, but what's the difference between with parenthesis and without? I don't see the difference.
Vercility Vercility

2018/1/29

#
danpost wrote...
Also, you may need to cast 'hp_change' as a double or the compiler may think it is an integer operation anyway.
Nah, iirc operations are always casted to the bigger type. Also tested this one, int*double returns double
Vercility Vercility

2018/1/29

#
DrCrane wrote...
danpost wrote...
Try removing the parenthesis around 'status_player_dmgoutput/100'. You are probably doing an integer operation, causing undo loss of precision. By multiplying first, it becomes a double operation, basically eliminating any loss..
That works, but what's the difference between with parenthesis and without? I don't see the difference.
You force the compiler to calculate the paranthesis first which is an integer calculation. By removing them, multiplication will act first and effectively cast the value to double Also, you should be aware that when you use double variables u can not always assume exact values. E.g 7.0*2.0 does not equal 14 but 14.00000000001 in Java.
DrCrane DrCrane

2018/1/29

#
Vercility wrote...
DrCrane wrote...
danpost wrote...
Try removing the parenthesis around 'status_player_dmgoutput/100'. You are probably doing an integer operation, causing undo loss of precision. By multiplying first, it becomes a double operation, basically eliminating any loss..
That works, but what's the difference between with parenthesis and without? I don't see the difference.
You force the compiler to calculate the paranthesis first which is an integer calculation. By removing them, multiplication will act first and effectively cast the value to double Also, you should be aware that when you use double variables u can not always assume exact values. E.g 7.0*2.0 does not equal 14 but 14.00000000001 in Java.
That does not make sense but guess I'll have to deal with it
Vercility Vercility

2018/1/29

#
What Exactly about that doesn't make sense to you
davmac davmac

2018/1/29

#
E.g 7.0*2.0 does not equal 14 but 14.00000000001 in Java.
Although it's true that floating point calculations can lose precision, this precise example is incorrect. 7.0 * 2.0 always equals exactly 14.0. Calculations on whole numbers up to a certain point will always be completely accurate. In fact, the floating point format correctly represents all integers up to a value of 16777216, though it may print less digits (eg. it writes that value as 1.6777216E7, which means 1.6777216 * 10000000, but internally stores every digit correctly). What floating point does have trouble with is exactly representing decimal fractions, such as 0.3 or 0.2.
Vercility Vercility

2018/1/29

#
IDK, used an online compiler on my phone when i wrote this, might have been faulty
You need to login to post a reply.