/    Sign up×
Community /Pin to ProfileBookmark

Mathematic calculation issue

1 – ((1624.00/0.56)/2900.00)
instead of giving output 0 (zero) the above code is giving output ‘1.1102230246252E-16’ in php

Someone please help.

Thanks
Saud …

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulAug 11.2015 — Hi and welcome to the site.

The problem is the order of calculation, and the rounding errors (which can arise in all computer languages that support floating point calculations). The easy solution is to re-order the calculation:

1 - ((1624.00/0.56)/2900.00)

becomes:

1 - 1624.00/(0.56*2900.00)

Which gives the result 0 because the brackets force the multiplication of the denominator to take place first, then the division and then the subtraction (BOMDAS is a mnemonic to help you remember the order of calculation).

Oops, have I just done your homework?
Copy linkTweet thisAlerts:
@NogDogAug 12.2015 — And just because there's almost always more than one way to do it...
[code=php]
<?php

bcscale(8);
$result = 1 - bcdiv(bcdiv(1624.00, 0.56), 2900);
var_dump($result);

[/code]
×

Success!

Help @saudrashid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...