octave-bug-tracker
[Top][All Lists]
Advanced

[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

URL:
  <http://savannah.gnu.org/bugs/?41742>

                 Summary: The rand function occasionally returns unexpected
results.
                 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

    _______________________________________________________

Details:

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
http://hg.savannah.gnu.org/hgweb/octave/file/c579bd4e12c9/liboctave/numeric/randmtzig.c#l400

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:
   
http://hg.savannah.gnu.org/hgweb/octave/file/c579bd4e12c9/liboctave/numeric/randmtzig.c#l392

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
them.

- Jordi G. H.




    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?41742>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

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