Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:02:02+00:00 2026-06-15T08:02:02+00:00

Is it surprising that the following code outputs a wrong value for startTime? public

  • 0

Is it surprising that the following code outputs a wrong value for startTime?

public class Temp {
    public static void main(String args[]){
       float duration = (float) 2.0;
       long endTime = 1353728995;
       long startTime = 0;
       startTime = (long) (endTime - duration);
       System.out.println(startTime);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:02:04+00:00Added an answer on June 15, 2026 at 8:02 am

    It doesn’t output the wrong value. It just outputs a value that you don’t expect.

    The important bit is what happens here:

    endTime - duration
    

    This is actually evaluated as:

    (float) endTime - duration;
    

    That’s where data is lost. The closest float value to 1353728995 is 1353729024, and the closest float to the result of subtracting 2 is still 1353729024.

    This all follows the language specification. JLS section 15.8.2 (additive operators) states that binary numeric promotions is applied to the operands.

    Section 5.6.2 (binary numeric promotion) starts off like this:

    When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

    • If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

    • Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

      • If either operand is of type double, the other is converted to double.

      • Otherwise, if either operand is of type float, the other is converted to float.

      • …

    So following those rules, the long value of endTime is converted to float, and then the subtraction is performed in float arithmetic.

    Remember that a float only provides 7 significant figures of accuracy, and the rest is fairly obvious, I hope.

    Note that without the cast of the result to long, the compiler makes it clearer what’s going on:

    Test.java:8: error: possible loss of precision
        long startTime = endTime - duration;
                                 ^
      required: long
      found:    float
    1 error
    

    That makes it fairly clear that the result is going to be a float, which should have raised warning bells about how the operation was going to be performed, and what accuracy might be expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following RX code: static void Main(string[] args) { int x = 0;
The following code does not compile: #include <iostream> class Foo { std::string s; public:
I find the following example mildly surprising: >>> class Foo: def blah(self): pass >>>
Please Explain the following code #include <iostream> using namespace std; int main() { const
I am trying to make a code that does the following: Multiplying the digits
I have code like the following, class _Process(multiprocessing.Process): STOP = multiprocessing.Manager().Event() def __init__(self, queue,
I have a method in a controller that looks like this: [HttpPost] public void
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
The following code produces random values for both n and v. It's not surprising
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.