Jump to content

bindec gives exponent


jBooker

Recommended Posts

Im trying to make a script for a game that will add custom options to an item. The options a made by taking binary, converting it to decimal and putting it in the database.

 

The binary is this:

 

First Awakening Type code (7 bits)  |  Sign bit for numeric value of first awakening (1 bit; 0 for + and 1 for -)  |  9-bit value of first awakening (9-bits)  | one 0 bit | Second Awakening Type code (7 bits)  |  Sign bit for numeric value of second awakening (1 bit; 0 for + and 1 for -)  |  9-bit value of second awakening (9-bits)  | one 0 bit | Third Awakening Type code (7 bits)  |  Sign bit for numeric value of third awakening (1 bit; 0 for + and 1 for -)  |  9-bit value of  third awakening (9-bits)  | 8 0-bits.

 

here are fours type codes:

Str code : 0000001

Dex code : 0000010

Int code : 0000011

Sta code : 0000100

 

Example: 0000001 | 0 | 111111111 | 0 | 0000001 | 0 | 111111111 | 0 | 0000001 | 0 | 111111111 | 00000000

which is STR + 511 | STR + 511 | STR + 511

 

 

when i convert that in calculator i get: 27004108590677760

when i use bindec() i get: 2.70041085907E+16

 

here is the code:

$itm = $_GET['itm'];
$slot = $_GET['slot'];
if(empty($_POST['val1']) OR empty($_POST['val2']) OR empty($_POST['val3']))
  { header("Location: index.php?a=char&id=$charid&itm=$itm&slot=$slot"); }
else 
  {
    $opt1bin = ($_POST["opt1"]);
    $val1dec = ($_POST["val1"]);
    $opt2bin = ($_POST["opt2"]);
    $val2dec = ($_POST["val2"]);
    $opt3bin = ($_POST["opt3"]);
    $val3dec = ($_POST["val3"]);

    $invrow = mysql_query("SELECT * FROM inventory WHERE charid = '$charid' AND itemid = '$itm' AND inventoryslot = '$slot'") or die(mysql_error());
    $invchk = mysql_num_rows($invrow);
     if($invchk == 1)
      {
       if($awakenaccess == 0)
       {
         if(($Get['accesslevel'] >= $gmcplvl) && ($Get['donated'] >= 0))
         {
          $val1bin = str_pad(intval(decbin($val1dec)), 9, '0', STR_PAD_LEFT);
          $val2bin = str_pad(intval(decbin($val2dec)), 9, '0', STR_PAD_LEFT);
          $val3bin = str_pad(intval(decbin($val3dec)), 9, '0', STR_PAD_LEFT);

          $awakebin = $opt1bin."0".$val1bin."0".$opt2bin."0".$val2bin."0".$opt3bin."0".$val3bin."00000000";
          $awakedece = bindec($awakebin);
          echo "$awakebin <br>";
          echo "$awakedece <br>";
          }
         }
        }
       }

 

Its not finished yet, it only echos the number for now untill i can get this problem solved.

 

How do I get this to give me to same results as calculator gives me, without the exponent?

Link to comment
https://forums.phpfreaks.com/topic/133698-bindec-gives-exponent/
Share on other sites

precision is likely lost because the number is too large for an int.

 

I guess you could technically calculate the change manually with BC Math, but that's slow and messy.

 

Why did you decide to do it this way?

 

Oh. ps. you could do sprintf('%.0f', $num) to get back the rough estimate of the number (even though precision has been lost).

How do you know that the value is actually different?

Just because the calculator is displaying every digit, while PHP is default displaying it in scientific format.

 

As genericnumber1 suggested, try

<?php

$value = 27004108590677760;

echo $value.'<br />';
echo sprintf('%.0f',$value);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.