Loss of precision - Java

user3125772

Problem Statement:

Write a method whatTime, which takes an int, seconds, representing the number of seconds since midnight on some day, and returns a String formatted as "::". Here, represents the number of complete hours since midnight, represents the number of complete minutes since the last complete hour ended, and represents the number of seconds since the last complete minute ended. Each of , , and should be an integer, with no extra leading 0's. Thus, if seconds is 0, you should return "0:0:0", while if seconds is 3661, you should return "1:1:1"

My Algorithm:

Here is how my algorithm is supposed to work for the input 3661:

  1. 3661/3600 = 1.016944 -> This means the number of hours is 1
  2. Subtract the total number of hours elapsed i.e. 1.016944-1=0.016944
  3. Multiply this with 60 i.e. 0.016944*60=1.016666 -> The number of minutes elapsed is equal to 1
  4. Subtract the total number of minutes elapsed i.e. 1.01666-1=0.01666. Multiply this with 60. This would yield the number of seconds elapsed.

The output produced however is 1:1:0. I tried to use a print statement and it appears that the value of 'answer3' variable is 0.999 and that is why prints the integer part (0). I tried to use the Math.ceil() function to round up the value and it produces a correct output. However I can only score about 60/250 points when I submit my code (TopCoder SRM 144 Div2) . Any insight for improving the algorithm will be helpful.

public class Time
{
public String whatTime(int seconds)
    {
        double answer1,answer2,answer3; int H;

        answer1=(double)seconds/3600;

        H=(int)answer1;

        answer2=(double)((answer1-H)*60);

        int M=(int)answer2;

        answer3=answer2-M;

        answer3=(double)answer3*60;
        int S=(int)answer3;

        String answer=Integer.toString(H);

        answer=Integer.toString(H)+":"+Integer.toString(M)+":"+Integer.toString(S);

        return answer;

    }
}
Hot Licks
public String whatTime(int seconds) {

    int secondVal = seconds % 60;
    int minutes = seconds / 60;
    int minuteVal = minutes % 60;
    int hours = minutes / 60;
    int hourVal = hours % 24;
    int daysVal = hours / 24;

    String answer = "" + daysVal + ":" + hourVal + ":" + minuteVal + ":" + secondVal;

    return answer;
}

Could do the formatting more elegantly, but that's the basic idea.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Loss of precision - Java

From Dev

Loss of precision in java

From Dev

Java Math cubic root loss of precision

From Dev

Possible loss of precision in java declaring bytes

From Dev

Initialize java byte array: possible loss of precision

From Dev

Precision loss with java.lang.Double

From Dev

Loss of precision?

From Dev

Java: Possible Loss of Precision When Using `Math.sin()`

From Dev

Is this a Povray precision loss artifact?

From Dev

Precision loss numpy - mpmath

From Dev

Precision loss with GMP

From Dev

Possible loss of precision error

From Dev

XYSeries autorange loss of precision?

From Dev

Loss of precision in fortran fft

From Dev

Python - Precision Loss With Large Integer

From Dev

Loss of precision - warning with usage of srand

From Dev

glReadPixels without loss of precision (Android)

From Dev

Loss of precision in float substraction with Swift

From Dev

Python - Precision Loss With Large Integer

From Dev

Loss of precision - warning with usage of srand

From Dev

Loss of precision in float substraction with Swift

From Dev

Django: Loss of float precision on save()

From Dev

Type conversions without loss of precision

From Dev

Avoiding Possible Precision Loss with a Simple Moving Average

From Dev

Loss of precision when using pow in C++

From Dev

Converting unsigned to double to unsigned without loss of precision

From Dev

What is causing this "possible loss of precision" error?

From Java

JavaScript to C# Numeric Precision Loss

From Dev

python float to string without precision loss