• 0

[Java] Double Multiplication


Question

In java when you multiply two doubles, often you won't get the result you were hoping for. For example 6*1.2 results in 7.19999... instead of 7.2.

Is there any way of sorting this out? You can round to 2 decimal places, I know, but what if the next multiplication needed 3 decimal places?

Thanks

Jether

Link to comment
https://www.neowin.net/forum/topic/607515-java-double-multiplication/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

It's a fact of life with binary computers. It's not just Java, it's the hardware. 1.2 ie one-and-one-fifth can't be exactly represented as a binary fraction. You get close with one-and-an-eigth-and-a-sixteenth.... but never exactly one-fifth.

You have to chose when/where to round the result, depending on the application.

  • 0

Example: 7.199999... properly rounded to 3 decimal places = 7.200

7.199999... * 10^3 = 7199.999999...

7199.999999... + .5 = 7200.499999...

Math.floor(7200.499999...) / 10^3 = 7.200

Does it work for all values? Let's find out!

3.14159265 to 2 decimal places:

3.14159265 * 10^2 = 314.159265

314.159265 + .5 = 314.659265

Math.floor(314.659265) / 10^2 = 3.14

-3.14159265 to 4 decimal places:

-3.14159265 * 10^4 = -31415.9265

-31415.9265 - .5 = -31416.4265

Math.floor(-31416.4265) / 10^4 = -3.1416

The stuff behind it:

public class RoundDemo {
	public static void main (String[] args) {
		double x = 4.294967296;
		double y;
		System.out.println("xx);
		for (int decimal_places = 8; decimal_places >= 0; decimal_places--) {
			y = x * Math.pow(10, decimal_places);
			y += Math.signum(x) * .5;
			y = Math.floor(y) / Math.pow(10, decimal_places);
			String formatString = new String("y=%." + Integer.toString(decimal_places) + "f\n");
			System.out.printf(formatString, y);
		}
	}
}

That's how I usually do it. Why don't I use the Math.round() method instead of doing the rounding myself? The answer is simple:

System.out.println("Math.round(4.51)\t Math.round(4.51)); //5
System.out.println("Math.round(-4.51)\t Math.round(-4.51)); //-5
System.out.println("Math.round(-4.50)\t Math.round(-4.50)); //-4
System.out.println("Math.round(4.50)\t Math.round(4.50)); //5

Wow. It would appear that the rounding is not fair! It actually is fair. It just doesn't round the numbers as expected. Is it a floating point issue? Possibly. However, this simple illustration shows how bad rounding can be when a certain amount of precision is absolutely required, which is why I prefer to round numbers myself.

Edited by rpgfan
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • iPadOS 26 is now official with improved windowing, menu bar, 'Liquid Glass' redesign, more by Taras Buria As expected, today, at WWDC 2025, Apple announced iOS 26 and other operating systems that now carry the 26 version number (for simplicity). iPadOS is also part of the big redesign, and it is now official with the new Liquid Glass design, improved window controls, a Mac-like menu bar, file management enhancements, and more. In addition to the general Liquid Glass redesign, which is now present across all of Apple's operating systems, iPadOS 26 received several iPad-exclusive upgrades. It now has a reworked windowing system that helps you organize and switch between apps. You can finally place and resize apps exactly how you want. Plus, iPadOS now has familiar window controls: three buttons for closing, minimizing, and resizing your app. Straight from macOS. Apps can now remember their previous size and position, and Expose can show you all open windows. Finally, iPadOS now has a menu bar, which is another thing that comes from macOS. It gives access to common actions, commands, and features. Developers can customize the menu bar in their apps and specify what features are available there. Apple is also upgrading the Files app with a reworked List view, which now shows more details about your files and the ability to expand or collapse folders. Plus, you can customize folders with colors and emojis, pin folders to the dock, and set default apps for file types. When working with long-running processes, background tasks show up as live activities, so that you can track Final Cut exports or the Files app moving stuff around. Other changes in iPadOS 26 include improved audio controls and the ability to select audio input source and record audio with voice isolation. The Journal app is now available on the iPad, Notes received markdown support, and Calculator now supports 3D graphing capabilities. You can learn more about iPadOS 26 in the official announcement post here.
    • That same Brave that tried to insert their own ads instead of blocked ones?
    • lol more spy centers - great work tiny hat gov. The surveillance state is licking it's chops so Palantir can know your every thought and move. 1984 is already here folks. Maybe start fighting it instead of contributing to it? Here is a good start - stop putting your entire life on a computer. Walk outside and see real life.
    • How long until Microsoft, Google, and others go (back) to this design trend?
  • Recent Achievements

    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      476
    2. 2
      +FloatingFatMan
      279
    3. 3
      ATLien_0
      255
    4. 4
      Edouard
      204
    5. 5
      snowy owl
      200
  • Tell a friend

    Love Neowin? Tell a friend!