octave-maintainers
[Top][All Lists]
Advanced

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

Re: New randi function added


From: David Bateman
Subject: Re: New randi function added
Date: Sat, 21 Aug 2010 18:32:10 +0200
User-agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706)

Rik wrote:
Jordi GutiƩrrez Hermoso wrote:
On 15 August 2010 17:04, Rik <address@hidden> wrote:
During the coding I came across a need to know the largest integer that
would fit within a given data class.  This was easy for the integer classes
(intmax("CLASS")) and for doubles there is bitmax().  Is there equivalent
functionality for singles?
Why not wrap C++'s <limits> header?

     #include <iostream>
     #include <limits>

     using namespace std;

     int main()
     {
       cout << "Max float: " << numeric_limits<float>::max() << endl;
     }

You can use is_iec559 if you want to ensure that float is 4 bytes,
although I can't imagine that anyone would be using a non-IEEE754
implementation of C++ for compiling Octave. I would expect that there
are already several assumptions in Octave sources that
numeric_limits<float>::is_iec559 is true.

It would appear that you are right about assumptions.  I looked up the
existing bitmax code in bitfcns.cc:526 and I find

retval = (static_cast<double> (0x1FFFFFFFFFFFFFLL));

which is surely a hard-coded constant.  On the other hand, configure checks
and adds compiler flags for IEEE754 behavior so perhaps you're right that
we are guaranteed IEEE754 conformance.

The <limits> code doesn't quite work as it determines the largest floating
point value that <float> can hold and I need the largest integer that
<float> can hold.  For that I need the number of bits in the mantissa of
the representation.

So, rephrasing the question.  Is there any issue with extending bitmax to
include a class argument and having the default for single be 24 bits of
precision.  (See
http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers)

--Rik

Its just 2^24 - 1 in the same manner that bitmax just returns 2^53-1. Why not just explicitly calculate this value in the same manner that bitfcn.cc(Fbitmax) does. That is

retval = (static_cast<float> (0xFFFFFF));

D.

--
David Bateman                                address@hidden
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)



reply via email to

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