Jump to content

[Solved] Php Subtraction Problem


1RV34

Recommended Posts

ProblemIt should return 39201661 but instead it returns 39201664.

<?php	$var = 76561197999467389;// will be dynamic	$sum = $var  - 76561197960265728;// will always be subtracted by the same value	echo $sum;// returns 39201664?>

What I've triedI've looked around & saw a function called bcsub() & tried to use that one but it returned 0.While looking around for an answer I also found out it might be caused by the storage of the numbers before subtraction. What I would like to knowIf it really is a storage problem, is there a workaround to get the right value anyway?If it isn't what might have caused it then? Edit: solved!

SolutionParse the values as strings and use bcsub().
<?php	$var = '76561197999467389';// will be dynamic	$sum = bcsub($var, '76561197960265728');// will always be subtracted by the same value	echo $sum;// returns 39201661?>

Link to comment
Share on other sites

Those are some really large numbers. Why do they have to be so big? Are you counting grains of sand on a beach? There's a chance that some of your numbers are out of the 32-bit range, though I haven't checked to make sure.

Link to comment
Share on other sites

yea they need to be so big & actually I don't think so either because I need it for an algorythm to convert a certain type of 64bit id to another type of id (but that one is a string & also I had no problems with the rest of the conversion, just this part)

Link to comment
Share on other sites

Have you made sure these values are integers and not floating point values?Try using intval() on the values before doing the subtraction. Also, can you tell why you need such large numbers. There might possibly be an easier solution.

Link to comment
Share on other sites

i might could change the order of the conversion so i can subtract by less but the first is returned by another server & not determined by me so i cant do anything about thati will try it with intval() tomorrow, now im on my phone & cant access a computer :P

Link to comment
Share on other sites

Try using intval() on the values before doing the subtraction.
I tried using intval() but it didn't help a thing at al.
PHP has functions for working with arbitrary-length integers: BC Math and GMP.
I already had tried bcsub() but it didn't work so now I tried gmp_sub() and that even returned that the function doesn't exist!Well I tried bcsub() again but this time I made the values strings & this time it did work! :DSolutionParse the values as strings and use bcsub().
<?php	$var = '76561197999467389';// will be dynamic	$sum = bcsub($var, '76561197960265728');// will always be subtracted by the same value	echo $sum;// returns 39201661?>

Thanks you both for your help!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...