Minecraft Blogs / Tutorial

Java Tutorial #7 - Real Numbers

  • 1,261 views, 1 today
  • 6
  • 1
  • 1
SourC00kie's Avatar SourC00kie
Retired Moderator
Level 45 : Master Blob
201
Introduction
_________________________________________

A number is referred to asn decimal when it displays a natural section and a precision. Both sectionsn are separated by a special character called the decimal symbol, whichn depends on the language and can be verified in the Regional Options dialogn box from the Control Panel:

derpherpderppng

The values on both sides of the decimal symbol are represented by digits. The value on the left side is expressed like a natural number: it can use a positive (+) or a negative (-) sign to show whether it is higher or lower than 0. It can also be written with commas to make it easier to read. The value on the right side of the decimal separator can be made of only digits. This value specify the level of precision allowed on the number. Normally, the more numbers, the more precise.

The Java language supports two types of real numbers: single precision and double-precision.

Double-Precision Values
____________________________________________________________

One of the main concerns with decimal values used in mathematics is their level of accuracy: the number needs to be as precise as possible. To declare a variable used to hold such a decimal number, you can use the double keyword.

To retrieve a double-precision value, you apply nextDouble to your Scanner variable.

To initialize a double variable, assign a decimal number to it. Here is an example:

public class Exercise {
public static void main(String[] args) {

double PI = 3.14159;
System.out.print("PI: ");
System.out.print(PI);
}
}

To ensure its accuracy, a double variable usually uses 64 bits to store its value.

In the parentheses of System.out.print() or System.out.println(), you can concatenate a string with a double variable. This is done using the + operator. Here is an example:

public class Exercise {
public static void main(String[] args) {
double number = 2685.404;
System.out.print("Number: " + number );
}
}

This would produce:

Number: 2685.404

You can also initialize a long variable with a hexadecimal number. Here is an example:

public class Exercise {
public static void main(String[] args) {
double number = 0xDDF402;
System.out.print("Number: " + number );
}
}

This would produce:

Number: 1.4545922E7

To display the value of a double variable using System.out.printf(), you have various options. You can use %g or %G. The compiler would use what is referred to as the general fixed number formal, which in US English uses two digits after the decimal separator. Here is an example:

public class Exercise {
public static void main(String[] args) {
double number = 2685.404;
System.out.printf("Number: %g", number );
}
}

This would produce:

Number: 2685.40

As an alternative, you can use the %f expression. If this case, the compiler would use a fixed standard format. Here is an example:

public class Exercise {
public static void main(String[] args) {
double number = 2685.404;
System.out.printf("Number: %f", number );
}
}

This would produce:

Number: 2685.404000

As you can see, the compiler would decide how many digits to display after the decimal separator, which is the period in US English. If you want to specify how many digits to display after the decimal separator, use an expression that follows this format:

%.Numberf

Notice the period after the % symbol. Between the period and f, you use the Number placeholder to enter the desired number of digits. Here is an example:

public class Exercise {
public static void main(String[] args) {
double number = 2685.404;
System.out.printf("Number: %.8f", number );
}
}

In this example, we used 8. This indicates to the compiler that we want to see 8 placeholders for digits after the decimal separator. In this case, there would be 8 placeholders. If the number includes 8 or more digits after the decimal separators, they would be displayed. If there are less than 8, there would be 0s in the remaining placeholder. The above code would produce:

Number: 2685.40400000

If you want to display the number in scientific (exponential) format, use %e or %E. Here is an example:

public class Exercise {
public static void main(String[] args) {
double number = 2685.404;
System.out.printf("Number: %e", number );
}
}

This would produce:

Number: 2.685404e+03

As you can see, the number appears with e. If you want the E (uppercase), use %E.

Single-Precision Values
________________________________________________________

A real number qualifies as single-precision when it needs only a limited number of bits, typically half of what is needed for a double, to show its value. The value typically fits in 32 bits and the precision is not a significant factor. To declare a variable that would hold decimal values with little to no concern with precision, you can use the float data type.

To retrieve a float value, apply nextFloat to your Scanner variable.

Because accuracy and precision are an important factor in mathematics, by default, when you declare a variable as float, you must explicitly let the compiler know that you are not concerned with precision and that the value would be dealt with single-precision. To specify this, when initializing the variable, type F to the left of the value. Here is an example:

public class Exercise {

public static void main(String[] args) {
float PI = 3.14F;
System.out.print("PI: ");
System.out.print(PI);
}
}

In the same way, when declaring a double-precision variable, to reinforce the idea of its values requiring double-precision, type d or D to the right side of its value. This would be done as follows:

public class Exercise {

public static void main(String[] args) {
double PI = 3.14159D;
System.out.print("PI: ");
System.out.print(PI);
}
}

Although a double variable uses more memory than the average variable, because memory is not expensive anymore, you should usually use the double data type more than the float when declaring a variable for a decimal value. Use float only if you must.

The float follows the same rules as the double regarding concatenation, hexadecimal, or formatting with System.out.printf().
Tags

Create an account or sign in to comment.

1
12/20/2011 7:07 am
Level 7 : Apprentice Network
PrinterPlayer
PrinterPlayer's Avatar
These tutorials are GREAT! +1 diamond for you
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome