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

  • SEARCH
  • Home
  • 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 6680075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:57+00:00 2026-05-26T04:25:57+00:00

When multiplying a floating point number that is very close to 1 with an

  • 0

When multiplying a floating point number that is very close to 1 with an int > 0, can it ever be interpreted as 1.

That is, if Math.random() returns its highest possible result (which is 1 step below 1.0), will

(int)(Math.random() * 8)

be 8 or 7?

For a practical example, can this often-used construct give an index out of bounds error:

someArray[(int)(Math.random() * someArray.length)];

I’m specifically interested in answers for Java and ActionScript 3, but I suppose they all use the same rules for floating point arithmetic, and answers for any platform would be useful.

Update: Though I already accepted an answer, I’d still appreciate confirmation that this can’t go wrong in ActionScript 3 either, since a colleague reporting that he saw it go wrong once is what partly prompted me to ask this question.

  • 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-05-26T04:25:58+00:00Added an answer on May 26, 2026 at 4:25 am

    If you multiply the greatest value below 1.0 with someInt (> 0), the result will never be someInt.

    This can be exhaustively tested for integers like this:

    Double greatestLessThanOne = Double.longBitsToDouble(4607182418800017407L);
    
    // Assert that greatestLessThanOne is indeed the largest double less than 1.
    //assert 1.0 == greatestLessThanOne + Math.ulp(greatestLessThanOne);
    
    for (int i = 1; i >= 0; i++)
        if ((int) (greatestLessThanOne * i) == i)
            System.out.println("Exception found: " + i);
    

    The snippet produces no output.

    (Math.ulp returns the distance between the given double and the double value next larger in magnitude. The assertion thus ensures that greatestLessThanOne is indeed the greatest value less than 1.0.)

    In other words, your line

    Object element = elementArray[(int)(Math.random() * elementArray.length)];
    

    will never give rise to an ArrayIndexOutOfBoundsException.


    Furthermore, according to Mark Dickinsons comment over here, this holds also when multiplying with a double.

    With IEEE 754 floating-point arithmetic in round-to-nearest mode, you can show that x * y < y for any x < 1.0 and any non-tiny positive y. (It can fail if y is either subnormal or the smallest positive normal number.)

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

Sidebar

Related Questions

Multiplying two binary numbers takes n^2 time, yet squaring a number can be done
Take an C++ integral variable i , and suppose that you're multiplying its value
A Measured value consists of (typically nonnegative) floating-point number and unit-of-measure. The point is
I need to multiply an integer (two's compliment) by a floating point constant. Here
I am trying to make a code that does the following: Multiplying the digits
I want to create a collection of classes that behave like math vectors, so
I stumbled across this oddity when multiplying DECIMAL numbers on SQL Server 2005/2008. Can
Can somebody explain why multiplying by 100 here gives a less accurate result but
I am multiplying the values of a few decimal columns together in SQL Server
Is there an algorithm for accurately multiplying two arbitrarily long integers together? The language

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.