POV-Ray : Newsgroups : povray.newusers : rounding Server Time
28 Jul 2024 20:30:04 EDT (-0400)
  rounding (Message 17 to 26 of 26)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: andrel
Subject: Re: rounding
Date: 28 Jun 2008 15:07:33
Message: <48668C29.90908@hotmail.com>
Warp wrote:
> Slime <fak### [at] emailaddress> wrote:
>> floor( A + 0.5 )
> 
>> (Although this will cause -3.5 to be rounded to -3, which I'm not sure is 
>> correct.)

you could use ceil(A-0.5) in that case or even use a switch for >0 and <0
> 
>   Why would rounding it to -4 be more correct? 3.5 is equally close to
> either value.
> 
depends on convention. There as thus 4 ways to round halves: 
up,down,towards zero and away from zero.


Post a reply to this message

From: Warp
Subject: Re: rounding
Date: 28 Jun 2008 15:09:51
Message: <48668c7f@news.povray.org>
andrel <a_l### [at] hotmailcom> wrote:
> depends on convention. There as thus 4 ways to round halves: 
> up,down,towards zero and away from zero.

  Actually there's a fifth convention which is also widely used: Round
to the closest even number. I believe the idea behind this one is that
tries to even out systematic bias in lengthy calculations.

-- 
                                                          - Warp


Post a reply to this message

From: andrel
Subject: Re: rounding
Date: 28 Jun 2008 15:17:05
Message: <48668E65.6080306@hotmail.com>
Warp wrote:
> andrel <a_l### [at] hotmailcom> wrote:
>> depends on convention. There as thus 4 ways to round halves: 
>> up,down,towards zero and away from zero.
> 
>   Actually there's a fifth convention which is also widely used: Round
> to the closest even number. I believe the idea behind this one is that
> tries to even out systematic bias in lengthy calculations.
> 
Not to mention the university teacher that recommended alternate up and 
down rounding for even less bias ;) . I have seen libraries of functions 
for the four I mentioned but not for this round to even or the 
alternating one or the random one you mentioned earlier.


Post a reply to this message

From: alphaQuad
Subject: Re: rounding
Date: 29 Jun 2008 14:05:00
Message: <web.4867ce3d42ba7542bfec89d20@news.povray.org>
didnt read it. I've seen less complex functions work and not work neagtive-wise,
depending on the sytem. handle negs, int() instead of floor() in POV should
solve the neg issue

#macro round(f,n)
  #local c = floor(pow(10.0,n));
  #local d = 0.5 / c;
  #local d = d + abs(f);
  #local d = d * c;

  #local d=floor(d);
  #local d = d/c;
  #if (f < 0.0) #local d = d * -1; #end
  (dd)
#end

functions rain overkill mode:

#macro incenter(A,B,C)
      #local a = C*vlength(A-B);
      #local b = A*vlength(B-C);
      #local c = B*vlength(C-A);
      #local d = vlength(A-B)+
                 vlength(B-C)+
                 vlength(C-A);
  #local result = (a+b+c)/d;
  result
#end
#macro centroid(A,B,C)
  #local result = (A+B+C)/3;
  result
#end

#macro circumcenter(A,B,C)
      #local a = vlength(A-B);
      #local b = vlength(B-C);
      #local c = vlength(C-A);
      #local sp = (a+b+c)*0.5;

   #local cosineR = ((b*b + c*c - a*a)/(2*b*c))*
      a*b*c/(4*sqrt(sp*(sp-a)*(sp-b)*(sp-c))); //circumradius(A,B,C);
   #local mp = (A+B)/2; //midpoint(A,B)
   #local result = mp+(vnormalize(vcross(A-mp,vcross(C-B,A-B)))*cosineR);
   result
   /* "walks" to cCenter in 3D.
      Algebra core step=cos(angle)*R.
      Input order irrelevant since both (A-mp)
      and norm vectors reverse in sync */
#end

#macro outcenter(A,B,C)
   circumcenter(A,B,C)
#end
#macro ccenter(A,B,C)
   circumcenter(A,B,C)
#end

#macro orthocenter(A,B,C)
      #local a = vlength(A-B);
      #local b = vlength(B-C);
      #local c = vlength(C-A);
      #local sp = (a+b+c)*0.5;

   #local cosineR = ((b*b + c*c - a*a)/(2*b*c))*
      a*b*c/(4*sqrt(sp*(sp-a)*(sp-b)*(sp-c))); //outeradius(a,b,c);
   #local mp = (A+B)/2; //midpoint(A,B)
   #local cc = mp+(vnormalize(vcross(A-mp,vcross(C-B,A-B)))*cosineR);

   #local result = ((A-cc)+(B-cc)+(C-cc)) + cc;
   result
#end

#macro inner_radius(A,B,C)
      #local a = vlength(A-B);
      #local b = vlength(B-C);
      #local c = vlength(C-A);
   #local result = 0.5*sqrt(((b+c-a)*(c+a-b)*(a+b-c)) / (a+b+c));
   result
#end
#macro tetrahedron_volume(A,B,C,D)
  #local result = abs(vdot(D-A,vcross(D-B,D-C))) / 6;
  result
#end
#macro pyramid_volume(A,B,C,D,E)
  // perimeter order for 1st 4 points
  #local result =
     tetrahedron_volume(A,B,C,E) + tetrahedron_volume(A,C,D,E);
  result
#end
#macro triangle_area(A,B,C)
     //2D .z must be 0
   #local result = abs((B.x*A.y - A.x*B.y)+
                       (C.x*B.y - B.x*C.y)+
                       (A.x*C.y - C.x*A.y)) / 2;
   result
#end
#declare inneradius = function(a,b,c) {
   //(3 lens)
   0.5*sqrt(((b+c-a)*(c+a-b)*(a+b-c)) / (a+b+c))
}
#macro circumradius(A,B,C)
      #local a = vlength(A-B);
      #local b = vlength(B-C);
      #local c = vlength(C-A);
   #local result = a*b*c/(4*sqrt(((a+b+c)/2)*(((a+b+c)/2)-a)*
                   (((a+b+c)/2)-b)*(((a+b+c)/2)-c)));
   result
#end

#declare outeradius = function(a,b,c) {
  //(3 lens)
  a*b*c/(4*sqrt(((a+b+c)/2)*(((a+b+c)/2)-a)*(((a+b+c)/2)-b)*(((a+b+c)/2)-c)))
  //(a*b*c) / (4*sss_area(a,b,c))
}

#declare sas_area = function(s,a,s2) {
   0.5 * s * sin(a) * s2
}

#declare sas_cos = function(s,a,s2) {
   sqrt((pow(s,2)+pow(s2,2))-2*s*s2*cos(a))
} // Side Angle Side - solve length of missing side


#declare sas_height = function(s,a,s2) {
    s*sin(a)*s2/sqrt((pow(s,2)+pow(s2,2))-2*s*s2*cos(a))
   //sas_area(s,a,s2) / (sas_cos(s,a,s2) * 0.5)
} // SAS height to missing side


#declare bh_area = function(b,h) {
   //(base,height)
   0.5 * b * h
}

#declare sss_height = function(a,b,c) {
  sqrt(((a+b+c)/2)*(((a+b+c)/2)-a)*(((a+b+c)/2)-b)*(((a+b+c)/2)-c))/(b*0.5)
  //sss_area(a,b,c) / (b * 0.5)
} // height to side2

#declare sss_area = function(a,b,c) {
   //semi perimeter
   sqrt(((a+b+c)/2)*(((a+b+c)/2)-a)*(((a+b+c)/2)-b)*(((a+b+c)/2)-c))
}

#declare vmyv = function {
//function that returns a vector
   transform {
     rotate <0, 0, 0>
     scale 1
   }
}

#declare aas_sin = function(a,b,s) {
   sin(b) * (s / sin(a))
} // AAS solve opposing side of angle2 (b)

#declare asa_sin = function(a,s,b) {
   sin(b) * (s / sin(pi-(a+b)))
} // ASA solve opposing side of angle2 (b)

#declare sss_cos = function(a,b,c) {
   acos((pow(a,2)+pow(c,2)-pow(b,2))/(2*a*c))
} // SSS solve angle opposite side2 (b)
  // if (pow(a,2)+pow(c,2)-pow(b,2))/(2*a*c) < 0 obtuse

#declare ssa_acute = function(s,s2,a) {
   asin(s2*sin(a)/s)
} // SSA solve angle opposite side2

/*
alias sss_area2 {
  ; (3 lens)
  return $calc(0.5 * ($1 + $2 + $3) * $inneradius($1,$2,$3))
}
alias sss_area3 {
  ; (3 lens)
  return $calc(($1 * $2 * $3) / (4 * $circumradius($1,$2,$3)))
}*/


Post a reply to this message

From: andrel
Subject: Re: rounding
Date: 29 Jun 2008 17:58:50
Message: <486805CE.2020701@hotmail.com>
alphaQuad wrote:
> didnt read it. 
read what?

> I've seen less complex functions work and not work neagtive-wise,
> depending on the sytem. handle negs, int() instead of floor() in POV should
> solve the neg issue
There is no "neg issue". People have different expectation on what e.g. 
floor() should do for negative numbers. It is more or less arbitrary, 
but it has to be defined. I agree that round() could be added as a 
function in POV4. Preferably not as a macro. Macro's tend to be written 
again and again. That is a waste of time and possibly dangerous as well 
if someone implements it subtly different from somebody else.


Post a reply to this message

From: Warp
Subject: Re: rounding
Date: 29 Jun 2008 18:03:02
Message: <48680696@news.povray.org>
andrel <a_l### [at] hotmailcom> wrote:
> People have different expectation on what e.g. 
> floor() should do for negative numbers.

  AFAIK the floor() function is universally defined to round downwards,
as the name implies. In other words, the return value is always smaller
or equal to the parameter.

> I agree that round() could be added as a function in POV4.

  Why? floor(value+.5) does the same thing.

-- 
                                                          - Warp


Post a reply to this message

From: andrel
Subject: Re: rounding
Date: 29 Jun 2008 18:31:28
Message: <48680D75.9030509@hotmail.com>
Warp wrote:
> andrel <a_l### [at] hotmailcom> wrote:
>> People have different expectation on what e.g. 
>> floor() should do for negative numbers.
> 
>   AFAIK the floor() function is universally defined to round downwards,
> as the name implies. In other words, the return value is always smaller
> or equal to the parameter.
yes (except perhaps on very old equipment that had a sign bit and not 
2s-complement), but that does not stop people from expecting that
floor(-x+.5)==-floor(x+.5) everywhere.
I was talking about expectations not definitions. (ok, 'would' in stead 
of 'should' would (should?) have been better)
> 
>> I agree that round() could be added as a function in POV4.
> 
>   Why? floor(value+.5) does the same thing.
> 
no it doesn't, that was the whole point. In some cases people want a 
function that round halves away from zero. round(-3.5)=-4


Post a reply to this message

From: Warp
Subject: Re: rounding
Date: 29 Jun 2008 18:54:59
Message: <486812c3@news.povray.org>
andrel <a_l### [at] hotmailcom> wrote:
> >   Why? floor(value+.5) does the same thing.
> > 
> no it doesn't, that was the whole point. In some cases people want a 
> function that round halves away from zero. round(-3.5)=-4

  Why would anyone want to round halves away from zero? And if someone
*really* wants that, he can make his own function which does that. The
rest can use either floor(value+.5) or ceil(value-.5), whichever they
prefer.

-- 
                                                          - Warp


Post a reply to this message

From: Alain
Subject: Re: rounding
Date: 30 Jun 2008 15:58:37
Message: <48693aed$1@news.povray.org>
andrel nous illumina en ce 2008-06-29 18:32 -->

> yes (except perhaps on very old equipment that had a sign bit and not 
> 2s-complement), but that does not stop people from expecting that
> floor(-x+.5)==-floor(x+.5) everywhere.

That expectation come from the fact that many peoples considere 0 to be smaler 
than -1, whitch is false. Many peoples have the tendency of ignoring the sign 
when quickly comparing values, only concidering the absolute value instead of 
the real one.


-- 
Alain
-------------------------------------------------
A short cut is the longest distance between two points.


Post a reply to this message

From: andrel
Subject: Re: rounding
Date: 3 Jul 2008 18:57:51
Message: <486D59A5.4050308@hotmail.com>
Alain wrote:
> andrel nous illumina en ce 2008-06-29 18:32 -->
> 
>> yes (except perhaps on very old equipment that had a sign bit and not 
>> 2s-complement), but that does not stop people from expecting that
>> floor(-x+.5)==-floor(x+.5) everywhere.
> 
> That expectation come from the fact that many peoples considere 0 to be 
> smaler than -1, whitch is false. Many peoples have the tendency of 
> ignoring the sign when quickly comparing values, only concidering the 
> absolute value instead of the real one.
> 
I don't think so. Anyway, this example did not express what I meant :(

BTW one reason for thinking that there may be a reason why people expect 
round(-3.5) to be -4 is that Matlab thinks so too.


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.