473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

large float's

According to php.net, the largest possible float is ~1.8e308.
wikipedia.org offers more insight, suggesting that is, exactly,
0x7FEFFFFFFFFFFFFF. This sorta begs the question... how can this
number be represented in PHP without resorting to the decimal notation?

<?
echo 0x7FEFFFFFFFFFFFFF;
?>

....doesn't work. Nor does...

<?
echo hexdec('7FEFFFFFFFFFFFFF');
?>

So... any ideas as to what will work?

Feb 23 '06 #1
2 4366
yawnmoth said the following on 23/02/2006 06:00:
According to php.net, the largest possible float is ~1.8e308.
wikipedia.org offers more insight, suggesting that is, exactly,
0x7FEFFFFFFFFFFFFF. This sorta begs the question... how can this
number be represented in PHP without resorting to the decimal notation?


The maximum floating-point value is platform dependent. For a 64-bit
platform, it's ~1.8e308, but for a 32-bit platform it's ~3.4e38
(assuming you're using the IEEE 754 standard).

This code converts from a hex-representation to a floating-point
representation (for 32-bit):
define("LENGTH", 32);
define("SIGN_BIT", 31);
define("EXPONENT_MSB", 30);
define("EXPONENT_LSB", 23);
define("MANTISSA_MSB", 22);
define("MANTISSA_LSB", 0);

function hex2float($strHex)
{
$dec = hexdec($strHex);
$sign = ($dec & (1 << SIGN_BIT)) != 0;
$exp = (($dec & ((2 << EXPONENT_MSB) - (1 << EXPONENT_LSB))) >>
EXPONENT_LSB)
- (1 << (EXPONENT_MSB - EXPONENT_LSB))
- (MANTISSA_MSB - MANTISSA_LSB);
$man = (($dec & ((2 << MANTISSA_MSB) - (1 << MANTISSA_LSB))) >>
MANTISSA_LSB)
+ (2 << (MANTISSA_MSB - MANTISSA_LSB));
$float = floatval($man * pow(2, $exp) * ($sign ? -1 : 1));
}
For a 64-bit version, you'll need to alter the constants; you can get
the required values from
http://stevehollasch.com/cgindex/coding/ieeefloat.html.

Note that this code doesn't deal with special cases, i.e. NaN, infinity,
etc.

--
Oli
Feb 23 '06 #2
Oli Filth wrote:
The maximum floating-point value is platform dependent. For a 64-bit
platform, it's ~1.8e308, but for a 32-bit platform it's ~3.4e38
(assuming you're using the IEEE 754 standard).


Actually the bit-ness of a CPU only refers to its integer handling
capability. Nearly all FPUs--even the ancient 8087--can handle both
single and double precision floating points.

Float points in PHP are defined as doubles, which in C are always
64-bit IIRC.

Feb 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Andrew | last post by:
What is the largest number (integer?) that I can work with in PHP? For example, I have this number: 1082655076035 This is actually a date that I need to compare with another date.
9
by: Eric Lilja | last post by:
Hello, consider the following two functions: /* function foo() */ void foo() { float y = 0.0f; float sum = 0.0f; for(int i = 0; i < num; ++i) {
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
5
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are...
8
by: Joe | last post by:
Hi trying to write some code to determine if user has large/small fonts set on PC thought this would work but it didn't Any suggestions??? Thanks Graphics g = Graphics.FromHwnd(this.Handle);
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
21
by: ico.bukvic | last post by:
Hi all, I've made a 2d dynamic array as follows (this is a snippet so not all variables are accounted for): //numvoices are dynamic (1-1000), entered by user //MAXCHANNELS is currently...
7
by: abunn | last post by:
I've been learning Visual C++ on the fly at work for the last 2 weeks and could use some help. I'm writing a program to read a .raw file which has a standard format of 9 columns and then depending...
1
by: superDk | last post by:
Hello, IE6 is adding a large margin between my two containers (container1 & container2) everything works fine in IE7 and Firefox. I've been searching on-line for a fix but I've had no luck. It...
5
by: Tzury Bar Yochay | last post by:
What is the reason math.pow yields OverflowError while python itself can calculate these large numbers. e.g: 1e+308 Traceback (most recent call last): File "<stdin>", line 1, in <module>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.