[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ast-users
Subject:    Re: [ast-users] Sign of floating-point zero...
From:       Roland Mainz <roland.mainz () nrubsig ! org>
Date:       2008-11-25 17:18:58
Message-ID: 492C3382.BD8F122A () nrubsig ! org
[Download RAW message or body]

Roland Mainz wrote:
> 
> Hi!
> 
> ----
> 
> Is there a reason why ksh93 does not display the negative sign for the
> value zero ? For example if I have use the C99 function "copysign"
> (copies absolute value of operant a and sign of operant b) I get this
> for { a=5, b=-0 }:
> -- snip --
> $ ksh93 -c 'float x; (( x=copysign(5, -0) )) ; printf "%f\n"
> x'
> -5.000000
> -- snip --
> 
> Now if I swap operands a and b I get this result:
> -- snip --
> $ ksh93 -c 'float x; (( x=copysign(0, -5) )) ; printf "%f\n"
> x'
> 0.000000
> -- snip --
> 
> AFAIK this result should be "-0.000000" ... or not ?
> 
> BTW: Parsing of "-0" doesn't seem to work either, e.g.
> -- snip --
> $ ksh93 -c 'float x a=-1 b=-0; (( x=copysign(a, b) )) ; printf "%f\n"
> x'

There is a small mistake: -0 is an integer value - it should be "-0." in
this case, e.g. the line should be:
-- snip --
$ ksh93 -c 'float x a=-1.0 b=-0.0; (( x=copysign(a, b) )) ; printf
"%f\n" x'
-- snip --

> 1.000000
> -- snip --
> ... while AFAIK it should be "-1.000000" since the 2nd operand of
> "copysign" defines the sign of the result.

BTW: As someone else pointed out the C99 spec for "copysign" allows some
"leeway", however my primary concern was that the shell does not seem
parse "-0.0" correctly and that $ printf "%a\n" x # does not return the
sign of zero.

For comparisation the following testcase...
-- snip --
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define SD(x) strtod(#x, NULL)

int main(int ac, char *av[])
{
	double a, b, result;
	
	a=SD(5.), b=SD(-0.);
	result=copysign(a, b);
	printf("a=%f, b=%f, result1=%f, %a\n", a, b, result, result);

	a=SD(-0.), b=SD(5.);
	result=copysign(a, b);
	printf("a=%f, b=%f, result1=%f, %a\n", a, b, result, result);

	a=SD(-0.), b=SD(-5.);
	result=copysign(a, b);
	printf("a=%f, b=%f, result1=%f, %a\n", a, b, result, result);

	return EXIT_SUCCESS;
}
-- snip --
... returns on Solaris 11/SPARC:
-- snip --
$ rm -f a.out ; cc -xc99=%all -lm
zerosign.c                                                                            \
 $
./a.out                                                                               \
 a=5.000000, b=-0.000000, result1=-5.000000, -0x1.4000000000000p+2
a=-0.000000, b=5.000000, result1=0.000000, 0x0.0000000000000p+0
a=-0.000000, b=-5.000000, result1=-0.000000, -0x0.0000000000000p+0
-- snip --
... and on SuSE Linux 10.0:
-- snip --
 ./a.out 
a=5.000000, b=-0.000000, result1=-5.000000, -0x1.4p+2
a=-0.000000, b=5.000000, result1=0.000000, 0x0p+0
a=-0.000000, b=-5.000000, result1=-0.000000, -0x0p+0
-- snip --

Somehow it looks only the shell looses the sign of a negative zero value
in this case.

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.mainz@nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-users mailing list
ast-users@research.att.com
https://mailman.research.att.com/mailman/listinfo/ast-users


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic