Hi Guys,

As the title says really... Sounds simple, it is.

But....! I'm dealing with huge numbers here. I'm dragging bandwidth from a logging system I am developing for a client and it returns it in BYTES... Very accurate :|

This is the sort of number I'm dealing with here...

$str = 1.797693134862316e+308;

$float = settype($str, "integer");

$bandwidthusage = intval($float) / 1048576;

echo $bandwidthusage;

The above code returns: 9.53674316406E-7

How do I convert this to MB?! It's driving me nuts. Any help would be really appreciated.

Thank you in advance,

Josh

Member Avatar for diafol

$str = 1.797693134862316e+308;

What the hell is this? I'd be impressed if you had this no. of bytes! ANyway, I think $float will be 1. So you'll always end up dividing 1 by 1048576!

settype, as far as I gather, simply changes the passed variable ($str). If you echo it, I think it'll be 1 (true).

So

settype($str, "integer");
echo $str;

I'd use ceil() to change the byte value. This means that the type will still be a float, but the integer contained therein can be a lot larger than the integer type allows.

*I think*

Thanks for your reply!

Don't ask! I'm working with Sawmill, and it returns bytes. It's for streaming video, that's why it's so big.

Well, this:

$str = 1.797693134862316e+308;

settype($str, "integer");

$bandwidthusage = ceil($str) / 1048576;

echo $bandwidthusage;

Returns 0

Where as this:

$str = 1.797693134862316e+308;

$bandwidthusage = ceil($str) / 1048576;

echo $bandwidthusage;

Returns INF

Any other guesses? I'm sure it can be done! :(

Member Avatar for diafol

http://www.php.net/manual/en/language.types.float.php#98216

Even if you are using terabytes, you won't get close to 10^308 bytes!

Change the power to 300, and it should work. I think you're too close to the platform limit for float. You do realise how absurd this 10^308 is? The number of atoms in the observable universe is only thought to be around 10^80!

Mark this solved soon as even my God-like mind is warping with such large numbers :)

<?php

//Start with the base string plus 15 decimal places
$str = '1797693134862316';

//Add 293 0's to the string e.g. (308 - 15)
for( $i=0; $i<293; $i++ ){
	$str = $str . '0';
}

//Use bcmath to divide it by 1048576
var_dump( bcdiv( $str, '1048576', 2 ) );

and the result....
17144137714980278015136718750000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000.00

Assuming that really is in KB, although I would take a guess it is really in bits or something.

The more I look at this, the more I have to wonder if your number is wrong.
1.797693134862316e+308 bytes would be 1.487016909281302219479708e+284 yottabytes.
1 bytes = 8.27180613 × 10-25 yottabyte

According to wikipedia the combined storage space of all harddrives in the world does not amount to 1 yottabyte.

Member Avatar for diafol

> According to wikipedia the combined storage space of all harddrives in the world does not amount to 1 yottabyte.

Ha ha ha! I'd like to see his internet hosting package!

Thanks guys... Yeh, I guess it must be wrong :| But the value coming from the api is <sc_bytes> so I presumed it would be in bytes. I'll have another bash today & see where I get & let you know. Thank you for your help, much appreciated :)

> According to wikipedia the combined storage space of all harddrives in the world does not amount to 1 yottabyte.

Ha ha ha! I'd like to see his internet hosting package!

I don't deal with bytes very often so give me some slack ;)

8 bits = 1 byte (B)
1024 bytes = 1 Kilobyte (KB)
1024 Kilobytes = 1 Megabyte (MB)

If you have the amount of KB and you want to convert it to MB, you just divide by 1024.

I'm not sure what you're doing with all those other numbers.

What is there not to get? Did you even read the rest of the thread? The op already stated clearly his number is supposedly in bytes. The problem isn't doing simple math the problem is doing simple math with ridiculous numbers.

I've come up with:

function bytesIntoMegaBytes($bytes)
	{
		// Convert Sawmills UBER bytes to Megabytes
		//$bytes = "1.797693134862316e+308";
		$str = (string)$bytes;
		
		$numofzeroes = 0;
		
		if(strstr($str, "e+"))
		{
			$str = explode("e+", $str);
			$num = $str[0];
			$powerof = $str[1];
			
			$num = str_replace(".", "", $num);
			$length = strlen($num) - 1;
			
			$numofzeroes = $powerof - $length;
		}
		
		//Add 293 0's to the string e.g. (308 - 15)
		for( $i=0; $i<$numofzeroes; $i++ )
		{
			$num .= '0';
		}
		
		//Use bcmath to divide it by 1048576
		return bcdiv( $num, '1048576', 2 );
		
		
	}

But that's giving me 2147483647.. At least I'm getting a more readable number. Still, I can't see them using 2147483647MB of bandwidth from 1st Dec to 14th Dec!

I'll keep going, and post my solution when I find it... :)

My mistake. It returns:

167423219872854277491569519042968750000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Member Avatar for diafol

What the heck are you trying to do? Your input byte integer will never be anything like the sort of numbers you're using.

Terabyte in bytes= terabytes * 1024 * 1024 * 1024 * 1024

Even with a bandwidth of 1 terabyte/s, your max no. bytes would be: 1099511627776 bytes/s

(1.0995 x 10^14 bytes/s).

When using a DB, we often limit integer and varchar fields. You should consider the same for your function parameters.

The highest 'usable' mega-expensive bandwidth I've heard of - and I'm sure this is well out of date by now - is 10Gb/s.

No. bytes/year = 338847149850624000 = 3.3885 x 10^17 (max use 24 hrs/day and using 365.25days/year)

I'd imagine, if you've got this sort of bandwidth, you wouldn't be cobbling together your own solutions, 'cos you'd be the richest guy in the Solar System.

$bandwidthusage = ceil($str / 1048576);

This should return an integer as long as your $str is less than 10^20 - which it must be.

Are you sure that huge number is bytes and not bits?

"The kilobyte is often considered to be 1024 (210) bytes in some fields of computer science and information technology. This use has been discouraged by the major standards organizations and a new prefix system was defined by the International Electrotechnical Commission, which defines the kibibyte for this binary multiple and affirms the kilobyte as 1000bytes. However, the new standard has not entered common usage, and has been actively resisted by some in the fields of computer science and information technology because of aesthetic objections to the new prefixes."

http://en.wikipedia.org/wiki/Kilobyte

I think you'll find that bandwith usage is still measured like that. Also, Google says 1,024: http://www.google.co.uk/#sclient=psy&hl=en&safe=off&q=1kB+in+bytes&aq=f&aqi=g1&aql=&oq=&gs_rfai=&pbx=1&fp=957dcd876369ec41 but that's besides the point.

Either way, bandwidth isn't measured in kilobytes/megabytes etc, it's measure in kilobits/megabits. Kbps = Kilobits per second (1,000 bits), KBps = Kilobytes per second (8,192 bits if we're saying 1 KB = 1,024 bytes and 8,000 if we're saying 1 KB = 1,000 bytes).

Member Avatar for diafol

I'm not bothering going into KiB/KB, just pointing out the irrelevancy of the big numbers being used - we're only factor 8 out every time if you want to be pedantic about bits/bytes. OP was discussing total bandwidth use for 3 months.

Nice table at the bottom for kibis/mebis/gibis/tebis for those interested here: http://www.t1shopper.com/tools/calculate/

Ok, agreed. It's a stupid number. That number is INF (1.797693134862316e+308)

I've since discovered that the API I'm dealing with, if the account has an error in it's bandwidth logs, it returns INF. I wrote a function to return false if it returns a number to the power of as that is, agreed, stupidly big.

It was in BYTES, and remains to be in BYTES. This is solved now, even though I had the answer at the start (Divide by 1048576... I decided to use BCDIV too over basic PHP math).

Thanks to everyone who helped out anyway, appreciated :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.