Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3322004
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:07:51+00:00 2026-05-17T23:07:51+00:00

For some reason, I’m having trouble negating a double and then returning it when

  • 0

For some reason, I’m having trouble negating a double and then returning it when a certain condition is met (which is being met). It stays positive, despite any coaxing otherwise.

 public static double angleDiffNoAbs(double from, double to, double maxValue) {  
  double diff = to - from;
  double absdiff = Math.abs(diff);

  if (absdiff > maxValue) {
   double newdiff;

   boolean clockwise = false;

   if (from < to) {
    newdiff = maxValue;
    clockwise = true;
    System.out.println("c");
   } else {
    System.out.println("ac");
    newdiff = maxValue * -1.0d;
   }

   System.out.println("from: " + from + ", to: " + to + ", diff: " + diff + ", absdiff: " + absdiff + ", newdiff(limited): " + newdiff + ", wound: " + (clockwise ? "clockwise" : "anti-clockwise"));

   return newdiff;
  }

  double result = ((180d + diff) % 360d) - 180d;
  if (result < -180d) result = 360d + result;

  return result;
 }

The result in my JUnit test is:

c
from: -10.0, to: 20.0, diff: 30.0, absdiff: 30.0, newdiff(limited): 10.0, wound: clockwise
ac
from: 20.0, to: -10.0, diff: -30.0, absdiff: 30.0, newdiff(limited): 10.0, wound: anti-clockwise

I’ve been looking at this for ages and can’t figure out the problem. The JUnit test is below (if it helps):

public class AngleDiffTest {
 @Test
 public void testAngleDiff() {
  double diff = Math.abs(Player.angleDiffNoAbs(353, 12, 360f));
  Assert.assertFalse("diff > 200: " + diff, diff > 200);
 }

 @Test
 public void testAngleDiff2() {
  double diff2 = Math.abs(Player.angleDiffNoAbs(360, 0, 360f));
  Assert.assertFalse("diff2 > 200: " + diff2, diff2 > 200);
 }

 @Test
 public void testAngleDiff3() {
  double diff2 = Math.abs(Player.angleDiffNoAbs(-10d, 20d, 10d));
  Assert.assertEquals("diff2 != 10.0: " + diff2, 10d, diff2);
 }


 @Test
 public void testAngleDiff4() {
  double diff2 = Math.abs(Player.angleDiffNoAbs(20d, -10d, -10d));
  Assert.assertEquals("diff2 != -10.0: " + diff2, -10d, diff2);
 } 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:07:51+00:00Added an answer on May 17, 2026 at 11:07 pm
    from: 20.0, to: -10.0, diff: -30.0, absdiff: 30.0, newdiff(limited): 10.0, wound: anti-clockwise
    

    This result appears to be from your fourth test case (testAngleDiff4), in which you’re passing a negative value of -10d for maxValue. When you do newdiff = maxValue * -1.0d, the two negatives cancel out, leaving you with a positive answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I never see this done. Is there a reason why not?
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its
For some reason, when I try to install SQL Server 2008 Express , I
For some reason, lately the *.UDL files on many of my client systems are
For some reason, Section 1 works but Section 2 does not. When run in
For some reason beyond me I can't access the mysql server on a machine.
For some reason the Windows command prompt is special in that you have to
For some reason after I installed Boot Camp, my os x terminal started to
For some reason, no matter how I go about it, I cannot get TortoiseSVN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.