user3125772
user3125772

Reputation: 73

Loss of precision - Java

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;

    }
}

Upvotes: 1

Views: 129

Answers (3)

Hot Licks
Hot Licks

Reputation: 47729

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.

Upvotes: 3

Robert Dodier
Robert Dodier

Reputation: 17576

Avoid floating point values, and work entirely with ints or longs.

Upvotes: 3

Eran
Eran

Reputation: 393821

You could solve this by working with ints :

  1. 3661/3600 = 1.016944 -> This means the number of hours is 1
  2. Subtract the number of hours * 3600 - i.e. 3661-(1*3600) = 61
  3. 61/60 = 1.0166666 -> The number of minutes elapsed is equal to 1
  4. Subtract the number of minutes * 60 i.e. 61-(1*60)=1. This yields the number of seconds elapsed.

Upvotes: 0

Related Questions