On 02/07/2007 02:14 PM, Andy Ross wrote:
>  > The patch itself looks sane and easy.  But I think I agree with
>  > John, this is a workaround for a design flaw in the step animation
>  > that we should just fix.

:-)


> So you want to pass in the floating point value 29.92 and a step of 10
> and get back 2, and *not* round up to 3.  You want truncation for
> everything but the final digit, where rounding is more appropriate and
> the bias value is a useful workaround.
> 
> So if there's a design flaw here, it's at a higher level.  Maybe
> what's really needed is a "digit extractor" animation instead.

Ah, that leads us to a point I previously sorta mentioned in
passing, but didn't really do justice to.

Life is only moderately complicated if we have the exact value
stored internally, and we can then round and truncate to form
external representation, without trashing the internal representation.
This is the only case I previously discussed.

Life is even more complicated if some comedian passes in an inexact
value such as 120.37, perhaps via something like --nav1=120.37
on the command line.

In such a case, it is necessary to channelize the number before
using it.  In the case of comm channels on 25 kHz spacing,
that means rounding to the nearest multiple of 25 kHz.

Below you can find the code to implement the required rounding.
It has some complexity beyond what is needed for the present
application, but is needed in order to properly round negative
quantities.

As before, we have a two-step process:

  a) Channelize the frequency, by rounding (not truncating)
   to the nearest exact channel value.

  b) Some (not all) instruments may choose to truncate the
   representation;  for what is internally 120.375 may be
   displayed as 120.37 externally.




# If arg is a multiple of half the quantum,
# it will be rounded *up* toward +infinity.
# This tries to preserve the property that rounding
# commutes with adding any integer multiple
# of the quantum, i.e.
#   round(arg+N*quantum, quantum) = N*quantum+round(arg,quantum)
# for all integer N
# However this may occasionally be violated because of the
# inherent inexactitude of the floating-point representation.
round = func(arg, quantum=1){
   if (quantum == 0) {
     return arg;
   }
   if (quantum < 0) {
     quantum = -quantum;
   }
   fudge = 0;
   if (arg < 0) {
     fudge = 1 + int(-arg/quantum);
     # Fudging is necessary because int() truncates toward zero
     # not toward + or - infinity.
   }
   return quantum * (int(0.5 + fudge + arg/quantum) - fudge);
}

print(0.5, " -> ", round(0.5), " should be 1");
print(-0.5, " -> ", round(-0.5), " should be 0");
print(120.37, " -> ", round(120.37, 0.025), " should be 120.375");

Result:

0.5 -> 1 should be 1
-0.5 -> 0 should be 0
120.3699999999999 -> 120.375 should be 120.375


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to