Math.Floor

T

Tukewl4u

Can someone direct me in the right direction on this? I just don't
understand what it's asking.
An Application of method.floor is rounding a value to the nearest integer.
The statement y = math.floor( x + 0.5 ); will round the number x to the
nearest integer and assign the result to y. Write an application that reads
double value and uses the preceding statement to round each of the numbers
to the nearest integer. For each number proceeded, display both the original
number and rounded number.
 
M

Manish Pandit

public static void testFloor(ArrayList<Double> list){
for(double d: list){
System.out.println("Original number is " + d + ", which is
floored to " + Math.floor(d+0.5));
}
}

-cheers,
Manish
 
M

Matt Humphrey

Tukewl4u said:
Can someone direct me in the right direction on this? I just don't
understand what it's asking.
An Application of method.floor is rounding a value to the nearest integer.
The statement y = math.floor( x + 0.5 ); will round the number x to the
nearest integer and assign the result to y. Write an application that
reads double value and uses the preceding statement to round each of the
numbers to the nearest integer. For each number proceeded, display both
the original number and rounded number.

Your homework assignment is asking you to write a program that reads a
double value and uses the given math statement to round each number to the
nearest integer. For each number you read in, print it and the rounded
number.

There really is no simpler way to put this--you'll have to talk to your
teacher.

http://home.earthlink.net/~patricia_shanahan/beginner.html

Do you know which compiler you're using? How to compile and run a program?
How to type in and edit source code? These are things your teacher or tutor
is best at helping you with.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

Jeffrey Schwab

Tukewl4u said:
Can someone direct me in the right direction on this? I just don't
understand what it's asking.
An Application of method.floor is rounding a value to the nearest integer.

I think it means: "One thing you can do with the Math.floor() method is
to round a number to the nearest integer."

Math.floor(d) returns the biggest (closest to positive infinity) number
that is less than d. See the documentation:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#floor(double)
The statement y = math.floor( x + 0.5 ); will round the number x to the
nearest integer and assign the result to y.

This says you can round a floating-point number to the nearest integer
by adding 0.5 (i.e. 1/2), then calling Math.floor.

Math.floor( 5.2 + 0.5); => 5.0
Math.floor( 5.5 + 0.5); => 6.0
Math.floor(-2.7 + 0.5); => -3.0
Write an application that reads
double value and uses the preceding statement to round each of the numbers
to the nearest integer.

It sounds like you're supposed to write a program that reads
double-precision numbers from standard input and rounds each of them
using "Math.floor(number + 0.5)". Getting the numbers out of the input
stream will probably be the hard part. My suggestion would be:

1. Wrap a BufferedReader around System.in.

2. Write a regular expression to represent whitespace, e.g:

Pattern whitespace = Pattern.compile("\\s+");

3. Read one line of input text at a time, using the BufferedReader's
readLine() method. Keep reading lines until readLine() returns null.
Each time you read a line, split it into tokens, e.g:

String[] tokens = whitespace.split(line);

Hopefully, each token you get will represent a number you're supposed to
round. You can get the numbers out of the tokens using
Double.valueOf(token).
For each number proceeded, display both the original
number and rounded number.

Easy stuff now. Just use the technique from the original problem
statement to round each number, and output it however you want. My
preference would be to wrap a java.io.PrintWriter around System.out and
use the printf method. Here is some code that includes nice formatting,
and which you can use on each token in turn.

private void showRound(String s) {
out.printf("%16s %16f\n", s,
Math.floor(Double.valueOf(s) + 0.5));
}

One piece of advice if you are new to Java, or to programming in
general: Start with a very small program that you can compile and run
without problems, even if the program doesn't do anything. For example:

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

}
}

As you add to your program, compile and run it often. This will help
you catch little problems before they become big problems.

If you have trouble, post your code here and the eagle-eye veterans will
help you find it.
 
T

Tukewl4u

Here's what I have so far. Am I on the right direction? Thanks

import java.util.Scanner;

public class RoundFinder
{
public void determineRound()
{
Scanner input = new Scanner( System.in );

System.out.print("Enter three values seperated by spaces: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();

double result = round( number1, number2, number3 );

System.out.println( "Rounded Value is : " + result);

}

public double round ( double x, double y, double z)

{
double roundValue = Math.round( x + 0.5);

if ( y > roundValue )
roundValue = y;

//if ( z > maximumValue )
// maximumValue = z;

return roundValue;
}
}
Jeffrey Schwab said:
Tukewl4u said:
Can someone direct me in the right direction on this? I just don't
understand what it's asking.
An Application of method.floor is rounding a value to the nearest
integer.

I think it means: "One thing you can do with the Math.floor() method is
to round a number to the nearest integer."

Math.floor(d) returns the biggest (closest to positive infinity) number
that is less than d. See the documentation:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#floor(double)
The statement y = math.floor( x + 0.5 ); will round the number x to the
nearest integer and assign the result to y.

This says you can round a floating-point number to the nearest integer by
adding 0.5 (i.e. 1/2), then calling Math.floor.

Math.floor( 5.2 + 0.5); => 5.0
Math.floor( 5.5 + 0.5); => 6.0
Math.floor(-2.7 + 0.5); => -3.0
Write an application that reads double value and uses the preceding
statement to round each of the numbers to the nearest integer.

It sounds like you're supposed to write a program that reads
double-precision numbers from standard input and rounds each of them using
"Math.floor(number + 0.5)". Getting the numbers out of the input stream
will probably be the hard part. My suggestion would be:

1. Wrap a BufferedReader around System.in.

2. Write a regular expression to represent whitespace, e.g:

Pattern whitespace = Pattern.compile("\\s+");

3. Read one line of input text at a time, using the BufferedReader's
readLine() method. Keep reading lines until readLine() returns null. Each
time you read a line, split it into tokens, e.g:

String[] tokens = whitespace.split(line);

Hopefully, each token you get will represent a number you're supposed to
round. You can get the numbers out of the tokens using
Double.valueOf(token).
For each number proceeded, display both the original number and rounded
number.

Easy stuff now. Just use the technique from the original problem
statement to round each number, and output it however you want. My
preference would be to wrap a java.io.PrintWriter around System.out and
use the printf method. Here is some code that includes nice formatting,
and which you can use on each token in turn.

private void showRound(String s) {
out.printf("%16s %16f\n", s,
Math.floor(Double.valueOf(s) + 0.5));
}

One piece of advice if you are new to Java, or to programming in general:
Start with a very small program that you can compile and run without
problems, even if the program doesn't do anything. For example:

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

}
}

As you add to your program, compile and run it often. This will help you
catch little problems before they become big problems.

If you have trouble, post your code here and the eagle-eye veterans will
help you find it.
 
J

Jeffrey Schwab

Tukewl4u said:
> An Application of method.floor is rounding a value to the nearest
> integer. The statement y = math.floor( x + 0.5 ); will round the
> number x to the nearest integer and assign the result to y. Write an
> application that reads double value and uses the preceding statement
> to round each of the numbers to the nearest integer. For each number
> proceeded, display both the original number and rounded number.
Here's what I have so far. Am I on the right direction? Thanks

You're off to a good start. There are some issues already, though. The
RoundFinder class cannot be run as a stand-alone program, for example,
since it has no main method.
import java.util.Scanner;

public class RoundFinder
{
public void determineRound()
{
Scanner input = new Scanner( System.in );

Good idea using a Scanner. It is probably the easiest way to get the
doubles. The reason I don't care to use it is that you lose the
formatting of the original input, but preserving the input formatting is
not one of the requirements of your assignment, anyway.
System.out.print("Enter three values seperated by spaces: ");

"Separated," not "seperated."
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();

double result = round( number1, number2, number3 );

System.out.println( "Rounded Value is : " + result);

Don't forget to output the original value, too. That was part of the
assignment.
}

public double round ( double x, double y, double z)

{
double roundValue = Math.round( x + 0.5);

That should be Math.floor, not Math.round.
if ( y > roundValue )
roundValue = y;

//if ( z > maximumValue )
// maximumValue = z;

Are y and z supposed to be the minimum and maximum permissible values?
If so, you probably want to give them more descriptive names and
comments. It also might be better to do the checking on the original,
input values if possible, rather than the rounded values, but that will
depend on what you want to achieve.
return roundValue;
}

I like to define the main function last.

public static void main(String[] args) {
new RoundFinder().determineRound();
}
 
L

Lew

One wrinkle is that Math.floor( x + 0.5 ) (not math.floor()) and Math.round()
will not perform standard IEEE rounding. The standard specifies the behavior
of Math.rint() - "If two double values that are mathematical integers are
equally close, the result is the integer value that is even."

In other words, if a double value x were exactly equal to 6.5, then
Math.rint(x) would return 6.0, but Math.round(x) would return 7L.

- Lew
 
T

Tukewl4u

How's this?

import java.util.Scanner;
import java.util.*;

public class RoundFinder
{
public void determineRound()
{
Scanner input = new Scanner( System.in );

System.out.print("Enter three values separated by spaces: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();

double result = round( number1, number2, number3 );
System.out.printf("(number1)= %f\n" , number1 );
System.out.printf("(number2)= %f\n" , number2 );
System.out.printf("(number3)= %f\n" , number3 );

System.out.println( "Rounded Value is : " + result);

}

public double round ( double x, double y, double z)

{
double roundValue = Math.floor(x + 0.5);

if ( y > roundValue )
roundValue = y;

if ( z > roundValue )
roundValue = z;

return roundValue;
}
}

OUTPUT:

Enter three values separated by spaces: 33.6
33.8
22.9
(number1)= 33.600000
(number2)= 33.800000
(number3)= 22.900000
Rounded Value is : 34.0


Jeffrey Schwab said:
Tukewl4u said:
An Application of method.floor is rounding a value to the nearest
integer. The statement y = math.floor( x + 0.5 ); will round the
number x to the nearest integer and assign the result to y. Write an
application that reads double value and uses the preceding statement
to round each of the numbers to the nearest integer. For each number
proceeded, display both the original number and rounded number.
Here's what I have so far. Am I on the right direction? Thanks

You're off to a good start. There are some issues already, though. The
RoundFinder class cannot be run as a stand-alone program, for example,
since it has no main method.
import java.util.Scanner;

public class RoundFinder
{
public void determineRound()
{
Scanner input = new Scanner( System.in );

Good idea using a Scanner. It is probably the easiest way to get the
doubles. The reason I don't care to use it is that you lose the
formatting of the original input, but preserving the input formatting is
not one of the requirements of your assignment, anyway.
System.out.print("Enter three values seperated by spaces: ");

"Separated," not "seperated."
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();

double result = round( number1, number2, number3 );

System.out.println( "Rounded Value is : " + result);

Don't forget to output the original value, too. That was part of the
assignment.
}

public double round ( double x, double y, double z)

{
double roundValue = Math.round( x + 0.5);

That should be Math.floor, not Math.round.
if ( y > roundValue )
roundValue = y;

//if ( z > maximumValue )
// maximumValue = z;

Are y and z supposed to be the minimum and maximum permissible values? If
so, you probably want to give them more descriptive names and comments.
It also might be better to do the checking on the original, input values
if possible, rather than the rounded values, but that will depend on what
you want to achieve.
return roundValue;
}

I like to define the main function last.

public static void main(String[] args) {
new RoundFinder().determineRound();
}
 
P

Patricia Shanahan

Tukewl4u said:
How's this? ....
public double round ( double x, double y, double z)

{
double roundValue = Math.floor(x + 0.5);

if ( y > roundValue )
roundValue = y;

if ( z > roundValue )
roundValue = z;

return roundValue;
}
} ....
....

What you wrote does not seem to match the problem you said you were
trying to solve. Did the specification change?

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top