[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #41742] The rand function occasionally returns

From: Michael Pender
Subject: [Octave-bug-tracker] [bug #41742] The rand function occasionally returns unexpected results.
Date: Sat, 01 Mar 2014 12:09:32 +0000
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.74.9 (KHTML, like Gecko) Version/7.0.2 Safari/537.74.9


                 Summary: The rand function occasionally returns unexpected
                 Project: GNU Octave
            Submitted by: sparx
            Submitted on: Sat 01 Mar 2014 12:09:31 PM GMT
                Category: Interpreter
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Incorrect Result
                  Status: None
             Assigned to: None
         Originator Name: Michael Pender
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
                 Release: 3.8.0
        Operating System: GNU/Linux



According to the documentation rand returns a number between (0, 1) and it is
conventional practice to be able to map the returned number to an integer
between 1 and a using an expression such as:

x = round (rand * a + 0.5)

However, on rare occasions rand actually returns exactly 1.0 which causes the
expression to return a+1 instead.  I've reviewed the code with Jordi Gutierrez
Hermoso, and I think the problem may be in lines 396 and 404-406 of the file

I am suspicious of the float cast and the addition of a constant.  Jordi has
other ideas which I will quote below, since I don't fully understand his
interpretation on the loss of precision issue.

- Mike


So I think I see the bug:

Here the cast to float is pointless. The constants are doubles, not
floats, so the computation is done in doubles, with 32 bits. However,
a float mantissa only has 23 bits, so casting to float loses precision
bits, and I think this casting also performs rounding.

I think it might be better to do bit manipulations to light the
appropriate mantissa bits in the float representation than to be
relying on implicit casts, explicit casts, and the arithmetic between

- Jordi G. H.


Reply to this item at:


  Message sent via/by Savannah

reply via email to

[Prev in Thread] Current Thread [Next in Thread]