On Wed, Jul 22, 2009 at 04:39:55PM +0000, Omari Stephens wrote:
> Frank van Maarseveen wrote:
> > On Wed, Jul 22, 2009 at 08:21:29AM +0100, Martin Ling wrote:
> >> On Wed, Jul 22, 2009 at 04:26:45AM +0000, Omari Stephens wrote:
> >>>> data->unnormalized_angle =
> >>>>     fmod((data->unnormalized_angle + 180.0) / 360.0) - 180.0;
> >>> Do you mean fmod(data->unnormalized_angle + 180.0, 360.0) - 180.0; ?  
> >>> fmod takes 
> >>> two arguments.
> >> Yes. ;-)
> > 
> > I've played with the idea too but it won't work. modulo 360 will yield
> > -360..360 which is a range of 720 degrees, not 360. Modulo 180 yields
> > the correct range but then the result is incorrect.
> 
> Yeah, the problem here is that fmod (and all "mod" operators/functions in 
> anything C-like) actually implements the remainder operation and not the 
> modulo 
> operation.  Basically, that means that the output range of fmod(x, N) is [-N, 
> N) 
> rather than [0, N) with the modulo operator.
> 
> This is one reason I hate C, and every other language that figured it'd be 
> good 
> to be compatible with C :o)  (Note that I have no idea if Fortran behaves 
> this 
> way or not 8)

Well, you just hinted me at the (C99) solution :-P

        data->unnormalized_angle = remainder(data->unnormalized_angle, 360);

   x         fmod(x,360)  remainder(x,360)
 170.000000:  170.000000  170.000000
 190.000000:  190.000000 -170.000000
-170.000000: -170.000000 -170.000000
-190.000000: -190.000000  170.000000
 350.000000:  350.000000  -10.000000
-350.000000: -350.000000   10.000000
 370.000000:   10.000000   10.000000
-370.000000:  -10.000000  -10.000000

test program:

        #include <stdio.h>
        #include <math.h>
        void main(void)
        {
                double f;
                while (scanf("%lf", &f) == 1)
                        printf("%f: %f %f\n", f, fmod(f, 360), remainder(f, 
360));
        }

fmod(x, y):
        The return value is x - n * y, where n is the quotient of x / y,
        rounded towards zero to an integer.

remainder(x, y):
        The  return  value is x-n*y, where n is the value x / y, rounded
        to the nearest integer.

notice the difference: towards zero vs. nearest integer.
It's not that important but I couldn't resist this puzzle :-)

-- 
Frank

------------------------------------------------------------------------------
_______________________________________________
ufraw-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ufraw-devel

Reply via email to