Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-11-2007, 05:38 PM   PM User | #1
dealmaker
Regular Coder

 
Join Date: Jul 2005
Posts: 342
Thanks: 0
Thanked 0 Times in 0 Posts
dealmaker has a little shameless behaviour in the past
Question Division by Zero Using Floating point. Why?

Hi all,
my code has the following equation and the number was gotton from the mysql database:

$num = - 5.5813651706861/ 1.8673620948785

But somehow I got the following errors:

Warning: Division by zero in /home/mysite/public_html/myfile.php on line 564

Do you know why?
Thanks.
dealmaker is offline   Reply With Quote
Old 07-11-2007, 05:59 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Something tells me the actual code you are running ends up with a zero in the denominator.
__________________
Fumigator is offline   Reply With Quote
Old 07-11-2007, 06:47 PM   PM User | #3
dealmaker
Regular Coder

 
Join Date: Jul 2005
Posts: 342
Thanks: 0
Thanked 0 Times in 0 Posts
dealmaker has a little shameless behaviour in the past
Question

No, I echo the variables and the numbers I included in the original post is the value of the variables. It's not 0. It's a really long floating number.

Quote:
Originally Posted by Fumigator View Post
Something tells me the actual code you are running ends up with a zero in the denominator.
dealmaker is offline   Reply With Quote
Old 07-11-2007, 07:17 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You can prevent the error with an "if" statement.

Also try casting the variables to float type.
PHP Code:
if ((float) $b == 0) {
    echo 
"Attempt to divide by 0.";
    
//handle the situation
} else {
    
$v = (float) $a / (float) $b;

__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:29 AM.


Advertisement
Log in to turn off these ads.