Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2002:
[Freeciv-Dev] Re: [PATCH] get rid of floating point calculations (PR#130
Home

[Freeciv-Dev] Re: [PATCH] get rid of floating point calculations (PR#130

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Petrus Viljoen <viljoenp@xxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: [PATCH] get rid of floating point calculations (PR#1309)
From: Petr Baudis <pasky@xxxxxxxxxxx>
Date: Mon, 11 Mar 2002 11:31:29 +0100

Dear diary, on Mon, Mar 11, 2002 at 11:31:05AM CET, I got a letter,
where Petrus Viljoen <viljoenp@xxxxxxxxxxx> told me, that...
> > > @@ -1105,7 +1105,7 @@
> > >                                    SINGLE_MOVE) * unit_types[d_type].hp;
> > >      }
> > >      d_val /= (unit_type(punit)->move_rate / SINGLE_MOVE);
> > > -    if (unit_flag(punit, F_IGTER)) d_val /= 1.5;
> > > +    if (unit_flag(punit, F_IGTER)) d_val -= d_val / 3;
> 
> This does not give the same results.
> You will loose data due to rounding
> 
> this would be better  d_val = (2*d_val)/3;
> The Compiler should optimize the (2*d_val) to a (d_val  << 1).
> 
> To verify (use bc (On Unix))
> 
> 47 / 1.5 ==>> 31
> (47*2)/3 ==>> 31
> 47 - (47/3) ==>> 32       this is due to a rounding error.!

Ouch.. I wondered if there won't be problems with this..

> >
> > >      freelog(LOG_DEBUG,
> > >           "%s@(%d,%d) looking for bodyguard, d_val=%d, my_val=%d",
> > >           unit_type(punit)->name, punit->x, punit->y, d_val,
> > > @@ -1555,7 +1555,7 @@
> > >              n = ai_choose_defender_versus(acity, punit->type);
> > >              v = get_virtual_defense_power(punit->type, n, acity->x, 
> > > acity->y) *
> > >                  unit_types[n].hp * unit_types[n].firepower *
> > > -                (do_make_unit_veteran(acity, n) ? 1.5 : 1.0) / 30;
> > > +                (do_make_unit_veteran(acity, n) ? 3 : 2) / 2 / 30;
> 
> It would be better to do
>                             unit_types[n].hp * unit_types[n].firepower  /
> (do_make_unit_veteran(acity, n)  ?  45 : 30)
> or
>                              unit_types[n].hp * unit_types[n].firepower  /
> (do_make_unit_veteran(acity, n)  ?  30*3/2  : 30)
> (The Compiler's optimization should do the constant folding for you...)

I don't think this is good. 30 is arbitrary constant and it should stay clear
that is it a constant and it should be also clear what the code does.

-- 

                                Petr "Pasky" Baudis

* elinks maintainer                * IPv6 guy (XS26 co-coordinator)
* IRCnet operator                  * FreeCiv AI hacker
.
"If you have acquired knowledge, what do you lack?
    If you lack knowledge, what have you acquired?"
Lev. R. 1:6
.
Public PGP key && geekcode && homepage: http://pasky.ji.cz/~pasky/


[Prev in Thread] Current Thread [Next in Thread]