473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to test for -0.0?

I am working on a function that takes degrees, minutes, seconds
coordinates and converts them to decimal representation. Traditionally,
in DMS notation the '-' sign, to indicate west longitude or north
latitude, precedes the degree value, e.i. -96° 59' 59". The float type
is capable of carrying a signed ZERO for the purpose of representing
between 0 and 1 degree west or south.

My problem comes from trying to select the correct algorithm for computing
a positive or negative value. To illustrate, here is the function:

double dms2dec(float degrees, int minutes, double seconds) {
if (degrees < 0 || degrees == -0.0) {
return (double)degrees - (double)minutes/60. - seconds/3600.;
} else {
return (double)degrees + (double)minutes/60. + seconds/3600.;
}
}

This function has an error in that it treats all instances of degrees,
whether -0.0 or 0.0, as negative and executes the first statement. If
degrees is less than or equal to -1 or greater than or equal to 1, the
function returns the expected result. If degrees is greater than -1 but
less than 0.0 the calculation is correct. When degrees is greater than
0.0 but less than 1, the error occurs.

My question, how can I properly test the degrees argument and make a
correct decision based on whether degrees is -0.0 or 0.0? Perhaps the
best solution involves the use of copysign() to store the sign, performing
the calculation as a positive value then signing the result, if necessary.

What do the gurus think?

- Nate >>

--

"The optimist proclaims that we live in the best of all possible worlds,
the pessimist fears this is true."

Nov 13 '05 #1
6 2633
On Wed, 10 Sep 2003 15:03:18 -0500, Nate Bargmann
<n0**@networksp lus.net> wrote:
double dms2dec(float degrees, int minutes, double seconds) {


Why not define
dms2dec(int degrees, int minutes, double seconds);

And have functions that return degrees reduce them to -180 to 180
or 0 to 360.

--
Coos
Nov 13 '05 #2
Just so everyone knows, I did get it working to my satisfaction by using
copysign() and then signing the result.

On Wed, 10 Sep 2003 23:34:16 +0200, Coos Haak wrote:
On Wed, 10 Sep 2003 15:03:18 -0500, Nate Bargmann <n0**@networksp lus.net>
wrote:
double dms2dec(float degrees, int minutes, double seconds) {


Why not define
dms2dec(int degrees, int minutes, double seconds);

And have functions that return degrees reduce them to -180 to 180 or 0 to
360.


Originally, degrees was an int, but GNU C doesn't sign the zero. This
function is part of a library project, so it's important to follow
established convention. To use int it seems as though a separate flag
value had to be used or the minutes or seconds values had to be signed.
Neither matches real world usage so the property of a float to carry a
signed zero works well.

Incidentally, my revised and working function:

double dms2dec(float degrees, int minutes, double seconds) {
double s, st;

s = copysign(1.0, (double)degrees );
st = fabs((double)de grees);

return copysign((st + (double)minutes / 60. + seconds / 3600.), s);
}

- Nate >>

--

"The optimist proclaims that we live in the best of all possible worlds,
the pessimist fears this is true."

Nov 13 '05 #3

"Nate Bargmann" <n0**@networksp lus.net> wrote in message
news:pa******** *************** *****@networksp lus.net...
I am working on a function that takes degrees, minutes, seconds
coordinates and converts them to decimal representation. Traditionally,
in DMS notation the '-' sign, to indicate west longitude or north
latitude, precedes the degree value, e.i. -96° 59' 59". The float type
is capable of carrying a signed ZERO for the purpose of representing
between 0 and 1 degree west or south.
(snip)
This function has an error in that it treats all instances of degrees,
whether -0.0 or 0.0, as negative and executes the first statement. If
degrees is less than or equal to -1 or greater than or equal to 1, the
function returns the expected result. If degrees is greater than -1 but
less than 0.0 the calculation is correct. When degrees is greater than
0.0 but less than 1, the error occurs.


For degrees/minutes/seconds form it would not be normal for degrees to be a
floating point type.

While floating point, on many machines, has the ability to store -0.0, it is
unusual for library conversion routines to generate it.

Your routine should accept a character string and do all conversions,
including the recognition of - or +, itself. You could also accept E or W
for east/west longitude, if that were useful.

-- glen
Nov 13 '05 #4
Nate Bargmann <n0**@networksp lus.net> wrote:

My question, how can I properly test the degrees argument and make a
correct decision based on whether degrees is -0.0 or 0.0? Perhaps the
best solution involves the use of copysign() to store the sign, performing
the calculation as a positive value then signing the result, if necessary.


C99 has a signbit() function to do the test directly.

-Larry Jones

My dreams are getting way too literal. -- Calvin
Nov 13 '05 #5
Nate Bargmann <n0**@networksp lus.net> writes:
Just so everyone knows, I did get it working to my satisfaction by using
copysign() and then signing the result.
Your code below also works on my hardware/implementation combination
(gcc and glibc on an i686). However, C99 does not guarantee its
portability. (C89 knows nothing of copysign().) From 7.12.11.1:

[#2] The copysign functions produce a value with the
magnitude of x and the sign of y. They produce a NaN (with
the sign of y) if x is a NaN. On implementations that
represent a signed zero but do not treat negative zero
consistently in arithmetic operations, the copysign
functions regard the sign of zero as positive.

As far as I can tell the standard does not require an implementation
to ever produce a negative zero in any floating type, nor (if it does)
to distinguish it from a positive zero in any meaningful or consistent
way. Things are different if your implementation defines
__STDC_IEC_559_ _, but gcc, for instance, does not, and claims
compliance with IEC 559 "if and only if the hardware is perfectly
compliant" (http://gcc.gnu.org/gcc-3.3/c99status.html).
On Wed, 10 Sep 2003 23:34:16 +0200, Coos Haak wrote:
On Wed, 10 Sep 2003 15:03:18 -0500, Nate Bargmann <n0**@networksp lus.net>
wrote:
double dms2dec(float degrees, int minutes, double seconds) {
Why not define
dms2dec(int degrees, int minutes, double seconds);

And have functions that return degrees reduce them to -180 to 180 or 0 to
360.


Originally, degrees was an int, but GNU C doesn't sign the zero. This
function is part of a library project, so it's important to follow
established convention. To use int it seems as though a separate flag
value had to be used or the minutes or seconds values had to be signed.
Neither matches real world usage so the property of a float to carry a
signed zero works well.


I don't follow the reasoning here at all. I can see that it's
important to allow the user to enter values using the established
convention, but that doesn't dictate your program's internal
representation of the value entered. To me, using float to represent
an integral quantity purely so you can get a negative zero is much
less natural than using a flag. That's just my opinion, though.
Incidentally, my revised and working function:

double dms2dec(float degrees, int minutes, double seconds) {
double s, st;

s = copysign(1.0, (double)degrees );
st = fabs((double)de grees);

return copysign((st + (double)minutes / 60. + seconds / 3600.), s);
}


And I'd leave out all the casts to double.

Rick.
--
R. A. Litherland: <li****@math.ls u.edu>
Web page: http://www.math.lsu.edu/~lither
Nov 13 '05 #6
Glen Herrmannsfeldt <ga*@ugcs.calte ch.edu> wrote:

But does the library guarantee to store a -0.0f if one puts -0 into scanf()?


Maybe. The standard doesn't explicitly say much about the conversions
done by scanf but it does refer to other library functions (e.g.,
strtol, strtod) to define the format of the input items, so it's
reasonable to conclude that those library functions also define the
conversions. In that case, strtod is required to honor the sign of zero
on implementations that support signed zeros.

-Larry Jones

I'm writing you a message in code. How do you spell "nincompoop "? -- Calvin
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3040
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
0
2029
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to...
4
3053
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and huge test classes, I started to think about splitting classes. Basically you have at least three obvious choises, if you are going for consistency...
0
1890
by: Andrea M. Segovia | last post by:
I just compiled (but did not install) perl 5.8.0 on an SGI Origin 300 server (IP35) running IRIX 6.5.20m. Make test reported one test error, which I narrowed down to .../lib/ExUtils/t/Constant.t using harness. I ran the test separately, and got the following detailed error report: .../lib/ExtUtils/t/Constant....ok 6/51Confused test...
0
2283
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** Invalid arguments to divll. at test_eng/Testout.pm line 30. ...propagated at t/polyser.t line 9. t/polyser.....dubious
4
9348
by: arotem | last post by:
Hi, I am trying to call an unbound method (PrintInput) with the object instance as the first argument but getting the following error: "TypeError: unbound method PrintInput() must be called with test instance as first argument (got test instance instead)" Below is the sample code (test) for this purpose (two files). Any help is greatly...
0
2301
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the example source code can be downloaded here: http://www.blogitek.com/timhaughton/archives/files/User%20Interrogator%20And%20TDD.zip The article...
6
2681
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following test-driven development, writing tests and then coding to satisfy them, I'll start with some of the simple tests for a class.
27
3752
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been ported from VB3 to VB6, a job which took over two years. We would like to port it to Python, but we need to continue to offer upgrades and fixes to the...
6
2618
by: ypjofficial | last post by:
HI, I have following terrific confusion. class test { public: int i; int * j; int **k;
0
7467
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.