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

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

[Top] [All Lists]

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

Petr Baudis wrote:

> Dear diary, on Sun, Mar 10, 2002 at 04:45:53PM CET, I got a letter,
> where Markus Linnala <maage@xxxxxxxxx> told me, that...
> >
> > Try to get rid of floating point calculations.
> >
> > diff -ur -X freeciv/diff_ignore freeciv/ai/advmilitary.c 
> > freeciv-rid-fp/ai/advmilitary.c
> > --- freeciv/ai/advmilitary.c  Wed Mar  6 12:05:09 2002
> > +++ freeciv-rid-fp/ai/advmilitary.c   Sun Mar 10 17:37:51 2002
> > @@ -59,7 +59,7 @@
> >    unit_list_iterate(map_get_tile(pcity->x, pcity->y)->units, punit)
> >      v = get_defense_power(punit) * punit->hp *
> >          (is_sailing_unit(punit) ? 1 : unit_type(punit)->firepower);
> > -    if (is_ground_unit(punit)) v *= 1.5;
> > +    if (is_ground_unit(punit)) v += v / 2;
> >      v /= 30;
> >      if (!igwall && city_got_citywalls(pcity) && is_ground_unit(punit)) {
> >        v *= l; v /= 10;
> > @@ -82,7 +82,7 @@
> >    int v;
> >    v = get_defense_power(punit) * punit->hp *
> >        (is_sailing_unit(punit) ? 1 : unit_type(punit)->firepower);
> > -  if (is_ground_unit(punit)) v *= 1.5;
> > +  if (is_ground_unit(punit)) v += v / 2;
> >    v /= 30;
> >    v *= v;
> >    if (!igwall && city_got_citywalls(pcity) && is_ground_unit(punit)) {
> > diff -ur -X freeciv/diff_ignore freeciv/ai/aiunit.c 
> > freeciv-rid-fp/ai/aiunit.c
> > --- freeciv/ai/aiunit.c       Wed Mar  6 07:27:57 2002
> > +++ freeciv-rid-fp/ai/aiunit.c        Sun Mar 10 17:37:51 2002
> > @@ -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.!


>
> >      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...)

>
> >              if (v * v >= d) { d = v * v; b = unit_types[n].build_cost + 
> > 40; }
> >            } /* let's hope this works! */
> >            if (!is_ground_unit(punit) && !is_heli_unit(punit) &&
>
> I'm ok with this.
>





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