# issues with a float

Printable View

• July 30th, 2011, 11:00 AM
2k.
issues with a float
Hi there,

I have decided to learn java programming, rather than working though a book which i find very dull i have just dived in by writing (trying to write) an application.

my application is for my other passion in life darts. so basically what the application does at the moment is calculates a players score from 501 down to 0 finishing on a double. this works fine but when i try to calculate the players 1 dart average i'm getting the wrong figure. To test this I did a 9 dart leg and the 1 dart average should be 55.67 however my code comes up with 55.0

I know that i'm probably missing something really obvious, i'm just at a loss to know what it is?

here is the code, appologies if anyone thinks its messy, it's the first programming i have done for about 14 years! :-"
Code :

```import java.util.Scanner;   public class DartApp { public static void main(String[] args){ //create a scanner Scanner scanner = new Scanner(System.in);   int total = 501; //to be changed depending on game int nthrows = 0; //number of throws to work out average float tdartave; //three dart average calculator   do{ System.out.print("enter player 1 score: "); int score = scanner.nextInt();   total = total - score; System.out.println("score is " +total);   //calculate number of throws if (total>0){ nthrows = nthrows +3;} else{ System.out.print("which dart was the winning dart 1,2,3 : "); int wdart = scanner.nextInt(); nthrows = nthrows + wdart; System.out.println("darts thrown = " +nthrows);} }while (total>0);   tdartave = (501 / nthrows); System.out.println("three dart average = " +tdartave);   } }```

cheers for any help that can be offered, and sorry if I appear thick
Mike
• July 30th, 2011, 11:32 AM
Norm
Re: issues with a float
You're probably having an issue with integer arithmetic. If variables in an expression are int, the compiler does int arithmethic. For example: 9/5 = 1 or 4/5 = 0
Use floating point numbers in the expression for the compiler to evaluate an expression as float.
1.0/2 = 0.5
or
(float)1 / 2
• July 30th, 2011, 11:46 AM
2k.
Re: issues with a float
hmm this is puzzling,

I have change the line
tdartave = (501 / nthrows);

to the following
tdartave = (501.0 / nthrows);

and i'm now getting this error
Code :

```DartApp.java:48: possible loss of precision found : double required : float tdartave = (501.0 / nthrows); ^ 1 error```

but what i don't understand is tdartave is a float :confused:
• July 30th, 2011, 11:50 AM
Norm
Re: issues with a float
501.0 is a double. Your code reduces its precision to a float.
There really is no reason to use a float.
Change your variable to a double.
Or make the number a float by adding a trailing f : 501.0f
• July 30th, 2011, 11:54 AM
helloworld922
Re: issues with a float
Java will default floating-point-like numbers as doubles (which have more precision than floats). You can force it to use floats by putting an 'f' after the number, or cast the final result to float.

Code Java:

```// either is acceptable tdartave = 501.0f / nthrows; tdartave = (float)(501.0 / nthrows);```

However, I would just change the type of tdartave to double and make life fun and easy :P
• July 30th, 2011, 12:03 PM
2k.
Re: issues with a float
cheers for the replies
by changing to
Code :

`tdartave = 501.0f / nthrows;`

I get the result 55.666668 which is perfect for the time being, having it rounding down to 55 the three dart average was out by 2.

I know the calculation was for a one dart average, but i was trying to find where the 2 points had gone ;)