Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Java Question... Hopefully Simple?


  • Please log in to reply
4 replies to this topic

#1 .Prodigy.

.Prodigy.

  • Members
  • 155 posts
  • OFFLINE
  •  
  • Location:New York
  • Local time:04:32 AM

Posted 21 January 2007 - 02:38 AM

I'm working on an ASCII version of battleship, and as part of the project, the hit/miss stats have to be printed out at the end. Because this is for a computer science course, my output has to be identical to the required output.

My program prints this:
Hit ratio: 66.66666666666667%
When it should be printing:
Hit ratio: 66.66666666666666%

and..
My Program:
Hit ratio: 33.333333333333336%
Required output:
Hit ratio: 33.33333333333333%

This is obviously a rounding error somewhere, but I have no idea how I would fix that.
Here's the code that computes the ratio:

double ratio = 100.0 * numHit / numFired;
Where numHit is a double representing the number of missiles that hit
and numFired is a double representing the number of missiles fired

Posted Image


BC AdBot (Login to Remove)

 


m

#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:32 AM

Posted 22 January 2007 - 07:43 AM

How are you formatting the numbers? In the first example, the rounding is correct. Count how many digits that the sample text has after the decimal compared to how many digits you have after the decimal. Are they the same? Are you currently studying anything like NumberFormat? There could be a few different reasons for your error. How are numHit and numFired being stored? Are they also doubles, or are they ints?

#3 Roger F. Gay

Roger F. Gay

  • Members
  • 90 posts
  • OFFLINE
  •  
  • Local time:10:32 AM

Posted 27 March 2007 - 07:32 AM

These ideas might provide a hint:

1.
Math.floor(num);


2.
divide(BigDecimal val, int roundingMode)
Returns a BigDecimal whose value is (this / val), and whose scale is this.scale().

ROUND_DOWN
public static final int ROUND_DOWNRounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value.


ROUND_FLOOR
public static final int ROUND_FLOORRounding mode to round towards negative infinity. If the BigDecimal is positive, behave as for ROUND_DOWN; if negative, behave as for ROUND_UP. Note that this rounding mode never increases the calculated value.


3.
Or cast to character string and then delete end characters:

// Delete characters
b.delete(16, 18); // Delete a range: "Now is the time"
b.deleteCharAt(2); // Delete 2nd character: "No is the time"
b.setLength(5);

Edited by Roger F. Gay, 27 March 2007 - 07:32 AM.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:32 AM

Posted 27 March 2007 - 07:37 AM

Do you suppose this guy fixed his problem by now?

#5 Roger F. Gay

Roger F. Gay

  • Members
  • 90 posts
  • OFFLINE
  •  
  • Local time:10:32 AM

Posted 27 March 2007 - 08:35 AM

Yes. But I always assume there are more people with the same question -- perhaps not as likely with this one as with others -- but the assumption keeps me rather blind to the posting dates of questions.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users