473,378 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Negative Zero Help

I am writing a program to find the coordinates of points on a unit circle. Everything works fine except for a minor aesthetic issue. Here is the code and then I'll discuss the problem:

Expand|Select|Wrap|Line Numbers
  1. public class PointsOnACircleV1
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         double radius = 1.0;
  6.  
  7.         double y1 = 0.0;
  8.         double y2 = 0.0;
  9.  
  10.         for(double x1 = 1.0; x1 >= -1.0; x1 -= .1)
  11.         {
  12.             y1 = Math.sqrt(Math.pow(radius,2) - Math.pow(x1, 2));
  13.             y2 = -y1;
  14.             System.out.printf("%6.2f%8.2f%14.2f%8.2f%n", x1, y1, x1, y2);
  15.         }
  16.     }
  17. }
  18.  
When x1 = 1.0 or -1.0, y2 = -0.0. Obviously this is the same thing as 0.0 mathematically, but it just doesn't look good in the output. An suggestions? I've tried using a if-else loop like this to remedy the problem:
Expand|Select|Wrap|Line Numbers
  1.             if(y1 != 0.00)
  2.             {
  3.                 y2 = -y1;
  4.             }
  5.             else
  6.             {
  7.                 y2 = y1;
  8.             }
  9.  
This will fix the problem for the first zero but not the second one. It completely baffles me. Hope that's all clear. Any help would be greatly appreciated.
Dec 10 '07 #1
4 3234
I'm pretty sure the problem here is that the number your working with is not actually 0, but rather a decimal >-.1 for example -.001, the negative comes in because it is a negative number, but with the format specifiers your ignoring those extra digits. for a nice easy fix I would suggest something like:
if(y2>-.1&&y2<0)
y2=0;
before your print. There are other ways (rounding/truncation) but thats nice and simple and I think all thats needed for your program.
Hope it helps
Dec 10 '07 #2
I'm pretty sure the problem here is that the number your working with is not actually 0, but rather a decimal >-.1 for example -.001, the negative comes in because it is a negative number, but with the format specifiers your ignoring those extra digits. for a nice easy fix I would suggest something like:
if(y2>-.1&&y2<0)
y2=0;
before your print. There are other ways (rounding/truncation) but thats nice and simple and I think all thats needed for your program.
Hope it helps
I removed the code that truncates the decimals and the number actually is zero. Or at least it's 0.000000. And when I do the math on paper I get zero for the problem values. This just baffles me.
Dec 10 '07 #3
JosAH
11,448 Expert 8TB
Here's something to read for both of you:

What Every Computer Scientist Should Know About Floating-Point Arithmetic.

kind regards,

Jos
Dec 10 '07 #4
BigDaddyLH
1,216 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. public class PointsOnACircleV1{
  2.     public static void main(String[] args){
  3.         double radius = 1.0;
  4.         for(double x1 = 1.0; x1 >= -1.0; x1 -= .1) {
  5.             double y1 = Math.sqrt(radius*radius - x1*x1);
  6.             double y2 = -y1;
  7.             if (y2 == -0.0)
  8.                 y2 = 0.0;
  9.             System.out.printf("%6.2f%8.2f%14.2f%20.15f%n", x1, y1, x1, y2);
  10.         }
  11.     }
  12. }
The first -0.00 is because there is such a thing as negative zero. This is not a "Java Thing" but part of how floating point numbers work. The second -0.00 is because of rounding error. As you can see when you run my version of the code, the last value isn't 0, it's -0.00000002107...
Dec 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: David Abrahams | last post by:
Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this result very surprising, and would just like to see the...
2
by: Dirk Hagemann | last post by:
Hi, Is there a datatype in python which allows no negative values? I subtract several times something from a value and I don't want to chek everytime if this value is still bigger or equal 0. ...
9
by: Elliot Marks | last post by:
Under what circumstances would a C program, containing nothing that is not standard C, prefix a zero result of a calculation with doubles with a minus sign? I have written a program that does...
4
by: August Karlstrom | last post by:
Hi, How come the modulus operator doesn't always yield non-negative values? I expect e.g. (-1) % 3 to be 2 but I get -1. If I want to decrement a cyclic variable n by k I have to write something...
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
19
by: Johs | last post by:
I need to make some special action if 'a' and 'b' are both positive or both negative. Is there some inbuilt function to check this? Johs
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
2
by: majie30 | last post by:
Write this program using functions. Write a program that accepts any number from the keyboard and tells you whether it is a nonnegative integer. The number should be sent to the function int_test(...
10
by: johnewing | last post by:
I am trying to figure out how to test if two numbers are of the same sign (both positive or both negative). I have tried abs(x) / x == abs(y) / y but that fails when one of the numbers is 0. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.