Jul 26, 2008

Php Is Doing Some Weird Calculations

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Php Is Doing Some Weird Calculations

Amezis
Well, I am making a script that is doing lots of different calculations. Everything works, until I see something very strange:

64 - 63.7035 = 0.29649999999999

It's completely obvious that this is wrong. The answer should be 0.2965... Why does it do such a stupid error, and how can I fix it?

Reply

Amezis
After some testing, I discovered that with this code, it would work fine:

CODE
<?php
$something = (64 - 63.7035);

echo ("$something");
?>


But my other code, which do the wrong calculation, is way longer. I don't know why... I've checked the variable, which should be 63.7035 and not 63.7035000000001, and it is correct.

By the way: It only happens with that number, I've tried with some other ones, but only the one I showed shows a wrong answer. I believe there's nothing to do with it...

Reply

beeseven
It's because of the way computers store numbers. They use binary, so sometimes you can't get a completely accurate number. The way that binary works in decimals is similar to the way it works in whole numbers:
CODE
463 = 256 + 128 + 64 + 8 + 4 + 2 + 1
Except for fractions and decimals, there are a lot of numbers that it is impossible to store exactly, because the denominator is not a power of 2. That's why with some calculations, you'll get the actual answer ± .000...001. It's easy to fix if you just round to several decimal places after you get the result, but unfortunately, there's no way to fix this at the source.

Reply

Amezis
Ok, thanks for your answer. But it doesn't really affect me since I am rounding the answer to closest whole number anyway, using the ceil() function.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : php, weird, calculations

  1. Weird Write Problem
    (3)
  2. Can't Access My Site....weird Issue
    (5)
    Okay, so last night everything worked fine. I loaded up cpanel, fixed issues with the site after the
    database move, etc. Today I'm unable to load up the site. It just keeps giving me ping
    timeouts. With what I'm being told by others it is only me having this issue with my site, and
    yet there have been 0 changes to my pc between it working and not. I have tried multiple times
    during the day with no luck. Along with this, if I load my website using a proxy service then I do
    not have any issues. The only time it will not allow me to access it is if I am "using" my....
  3. Scam Email Or I'm Rich
    Weird email saying I won UK lottery (11)
    I for some reason recently received an email saying I won the UK lottery for the sum of 1,000,000
    GBP. It is obviously a scam or I'm rich. Here is the email for your inspection: QUOTE
    From:popowong@singnet.com.sg To:info@claimsagent.uk Message: *REFERENCE NUMBER:UK/786543X4/28*
    *BATCH NUMBER:034/099/YX46* *TICKET NUMBER:005-4432-971-878* *FINAL NOTIFICATION * *We are pleased
    to inform you today 13th April 2008 of the result* *of the winners of the UK NATIONAL LOTTERY
    ONLINE PROMO PROGRAMME,* *held on April 2008 .You won the sum of £1 000,000.00 GBP* *file X....
  4. My Dream Last Night.
    WEIRD (5)
    Ok. Well I don't usually remember my dreams, but this morning I did for some reason and it is
    fading away fast! lol It was a WEIRD dream ok? First of all there was a snow bus...yes a snow
    bus all right!? And there was me,my girl friend, and Joey from the show Friends...don't ask
    me why he was there! But someone asked if Joey wanted to go on the snow bus and he was like
    SURE! and then blah blah blah he finally got on and then the bus started skiing down the
    Aplines! And Joey started freaking out because he thought the snow bus was something ....
  5. Smf Help On Qupis Account
    weird problem (2)
    This hosting is great, and every problem somehow solves itself on it's own somehow here... But i
    have one more problem. I have installed SMF forum a few times, and i know i did it right, because i
    installed it a few times on some other hostings. But on this one i get some error. I usually get it
    when i want to login to administration panel, when i want to install some new plugins(called
    "packages" in smf) or when i want to setup a new section in forum(i don't know how to explain it
    in english)... Anyway, the problem is, when i try to do some of those things, it c....
  6. Canadian Tax Software
    Free Tax Calculations and e-file (0)
    http://www.studiotax.com/en/main.htm Download the StudioTax Canadian Personal Income Tax software
    from the above link. Fantastic piece of software. The input forms mimic the actual paper documents,
    line numbers are the same, easy to follow directions, in general, a really useful piece of software.
    Approved for electronic submission to speed the process. No paper to mail in. (Although you should
    save the receipts and stuff for 7 years) Allows for all the Standard forms, like for reporting
    self-employed Income, etcetara. It doesn't work for all returns, but I imag....
  7. Woman Stays In Bathroom For 2 Years
    This one is weird (11)
    http://www.cnn.com/2008/US/03/13/woman.in....m.ap/index.html
    http://news.yahoo.com/s/ap/20080312/ap_on_...man_in_bathroom (They're the same article, only
    one lists the actual author) Ooh, hey, video:
    http://www.cnn.com/2008/US/03/13/woman.in....tml#cnnSTCVideo Ok, so this woman ended up locking
    herself in the bathroom for 2 years. Now one would think, "Ok, how could someone be THAT mad for
    THAT long?" Turns out, she had some strange fear of leaving the bathroom because of a bad
    upbringing. She ended up getting stuck on the toilet seat for a month and the para....
  8. Weird And Wonderful Computer Furniture
    (8)
    Well I saw this I have to those are some sweet set ups, although it begs the question if they are
    worth it or not or if you have the room to set them up. 1. Vision One Computer Workstation
    QUOTE Want a workstation as comfortable and ergonomically healthful as the cockpit of your car (or
    the car you wish you had)? Look no further than the Vision One line, which comes equipped with your
    choice of genuine car seats in various colors, finishes, and configurations. The 12-way-powered
    Porsche leather bucket seat (pictured) is the most popular upgrade; Corvette and V....
  9. Is This Normal With A Fully Loaded Page
    Status bar acting weird (3)
    I made a form with Coffeecup Form builder and embedded it in a HTML file. I have now uploaded all
    the files to the server (in a separate folder). The page works, all functions work OK, but when the
    page is fully loaded, in the status bar it keeps saying: "Read www.vlaanderen-flanders.org.uk". Is
    that normal? Or is there a fault somewhere? Do I have to do something about this? Thanks. ....
  10. Weird Password Problem
    Password change failed (8)
    I haven't really used my cPanel because my hosting is mainly used for a mailing list (for the
    time being). When I headr of the potential password breach I tried to log on but my attempts failed.
    I know the details are right since I saved the details in a txt file. Even more peculiar is that I
    can still log in via FTP. I tried to reset my password but got a error message stating that my
    password could not be changed. Any insight would be much appreciated.....
  11. Sounds To Alter Moods - Idoser.com - Doses
    A New, Weird Experience. (2)
    So, today my neighbor, named Dan came over. We talked about video games for a while, but following
    this talk he brought upon a new topic. He started talking about a sound program that "got him high."
    This was a very curious subject for me because I had never heard such a thing, so I continued to
    listen to him. He showed me the site http://www.i-doser.com/ were his experiences began. Binaural
    Brainwave Dosing is the name of the subject, with the word brainwave I became even more interested.
    I guessed what the program would do, was through audio, alter brain waves. I rea....
  12. Weird Google Map Result
    swim across atlantic ocean (14)
    http://maps.google.com/maps?f=q&hl=en&...265625&om=1 Check out step 24... HA!....
  13. Grunge Sign
    weird but nice (4)
    i think u have to have a personal thing for this kindastyle, i love grungy styles in a signature,
    thats why i tried it for myself and this is the outcome, i'll give u an explanation how i done
    this render used= 3 other images used=2 layer styles=7 brushes used=none burn tool used=yes
    everylayer dodge tool used=yes everylayer....
  14. Boinc - Donate Your Excess Cpu Calculations For Science
    [fixed name in Topic title] (4)
    I leave my computer on all the time and it just sits there useless. I figured it could be used for
    something good and found boinc, where you can have your cpu calculate for different science projects
    when you're not doing anything on it. It can search for extraterrestrial intelligence, help
    decode genomes, help map out the most efficient way to prevent malaria or many others. Try it out
    and help the world without any effort, what easier way to give yourself that warm fuzzy feeling.
    I've already gotten 881 blocks processed for Seti and still working at finding ali....
  15. Css Trouble
    ul submenu displays weird in IE (7)
    I have asked for help in the css creator forum, but I haven't got an answer. Well some one
    actually posted something but didn't work, so I will post my trouble here to see if somebody can
    bring light... thanks in advance... QUOTE Menu in css, shows fine in the good browsers. I
    use the suckerfish dropdown js principle to have a vertical menu with a submenu poping out. I think
    I have some issues as to xhtml or css around all over the index, but the trouble I can't fix is
    the submenu acting weird in IE6 and it looks like in IE7 also. When you hover over....
  16. Weird Girl Problems!
    (12)
    Hey, I'm new here. Basically, I'm 23years old. To cut a long story short here's the
    deal. I have a very close friend and he means a lot to me. He's helped me in many, many ways,
    both direct and indirect. I have a LOT of respect for him and am always looking out for him.
    He's basically a Badminton coach and I train with him every morning, and see him every evening.
    He's also my badminton coach, but more than anything else, a friend and someone to look up to.
    We hang out a lot and visit movies etc quite often. I enjoy his company, and I'm sure ....
  17. Router Is Acting Weird
    After a power failure (8)
    Hello people, This week we've had a power failure arround the entire neighbourhood. Altrough
    the power was restored after a few hours my computer has a big problem now, basicly, if I let it
    inactive for too long (and sometimes randomly) the router disconnects my PC. When I try to reconnect
    to the network after that I get an error message saying something like my PC cant get a network
    adress assigned. After reconnecting / disconnecting the network cables and reinstalling the drivers
    from my network card it usually starts working again, for a few while. Now I'm af....
  18. Weird But Fun Games
    We should make a list (3)
    There are lots of games on the internet that sound weird, stupid, pointless, or just too simple to
    be any good, but turn out to be really fun and addicting. If you come across any such game post it
    here and we can make a big list. I'll start: Falling Sand Game -
    http://www.trap17.com/forums/falling-sand-game-t40071.html (description in link) Mini Putt -
    http://www.addictinggames.com/miniputt.html (Just a game of mini-golf) Plasma Pong -
    http://www.plasmapong.com (pong with fluid dynamics and whatnot - the fluid dynamics test mode is
    really fun) There....
  19. Need Help With Next Step With This Girl
    Weird situation, don't know where to go (10)
    Alright, i'm usualy a more shy person, but yesturday, i was drunk out of my mind, went out
    smoking and met this girl, i can't remember how i started, but after a while i was hugging her
    every minute and even got on my knees asking her not to go when she went half an hour later. I also
    got her number sometime that night that I can't remember. Anyways, the next day she was telling
    everyone around about how funny I was and wanted to see me again. Now being the shy person I am,
    I'm not sure how to go about pulling off the same, and don't wont her to lose ....
  20. Weird Al Yankovic
    SO F**CKING FUNNER (0)
    Weird al yankovic this is so god damn funny PLAY i love it soo much....
  21. Weird Error
    (3)
    I was trying to upload some stuff to update my site, until I saw this QUOTE kioku - You are out
    of space to upload this file It appeared straight in a row a good couple of times, even though I
    tried and tried again. The weird thing about this error is that I still have about 5 MB left.
    Anybody have any idea of how I could make it stop giving me this error and allow me to upload stuff
    once more?....
  22. Weird Ps2 Disc Reading Problem
    Works only at 1st time... (13)
    I dont have a problem with my PS2... Anyway, I have this game; SW Revenge of Sith . I played
    it much and one day it simply stop working, no read! Surface of disk was clean, so I blamed the
    sound system (my box were standing near the buffer) . Now Im keeping my games away from
    magnetic fields :) A few days ago, I put the game and it worked ! I played a little. I was
    so happy... Later I called my brother to show him, but when put the game again, no read! I
    figured out now. That game disc only works when the machine is cold/fresh/just opened. ....
  23. How Do You Stop A Girl From Liking You?
    Wow this topic must be weird with a title like that! (42)
    There is this girl in my core class (the class that I do maths, english and stuff like that with)
    and she likes me, the only problem is that she is a bit.... well 'open' (that can get very
    annoying) about it, and I already go out with someone! How do youdo this without hurting your
    rep? No she don't listen to me when I ask her to stop being so open about it.........
  24. Cms Made Simple
    A weird experience for this Mambo user (10)
    I came across yet another CMS called CMS Made Simple (I'll just call it CMSMS from here on). Of
    course, it is free and open source, and it is very compact (its latest stable distribution is 1.89
    MB zipped, Mambo's is a lot larger, like 8 or more MB proably). Any page loads quickly, and are
    clutter free. Part of CMSMS's features are its templating system, which works pretty
    differently. In Mambo, there are the index.php template, and its accompanying template_css.css
    files. In CMSMS, there's the template (stored in the DB as far as I know), and a whole bun....
  25. Is It True Or Just A Weird Email ?
    hotmail ID can be freeze ??? (26)
    Dear last night a very strange and very disgusting thing happend to me . It so happened that last
    night i tried to SIGN-IN my hotmail id using messenger , bet every time when i tried to sign-in I
    recieve a message off INVALID password . I just tried to sign-in in my Inbox but this time i
    recieved a error page that your account is temperarly unavaiable. I tried much but in vain . I just
    think that there may be some problem in my internet .I tried to sign-in with my other ID this time
    with other ID i was sucessfull . then i just tried with the previous id but i was conti....
  26. Cloning Animals To Reduce Obesity!
    This is a weird one. (7)
    I was watching the news a while back and i couldnt help but keep my eyes peeled when i saw something
    about cloning animals to get a nutrient that reduces the fat production in youre body.. It turns out
    that a chromosone in your DNA regulates the fat production in youre digestive system. The chromosone
    i believe was Omega 5 or something along those lines.. I cant really remember, But anyway... They
    tested it out by injecting the nutrient into an egg of a rat, and when the rat was born, it
    contained the nutrient. This shocked me.. "We are going to start eating cloned anima....
  27. Weird Email
    (3)
    /ohmy.gif" style="vertical-align:middle" emoid=":o" border="0" alt="ohmy.gif" /> Today, I opened up
    an account in Gmail, and I finally get the immense 2 gig space. Then I open up my email and
    bam!!! An email to me from the Bank of America. The weird thing is that I don't use
    Bank of America. /ph34r.gif" style="vertical-align:middle" emoid=":ph34r:" border="0"
    alt="ph34r.gif" /> So I retraced the email adress and it said (unknown). Any comments please host.....
  28. Weird E-mail I Got Sent
    Take a Look, it's a scam, for sure (2)
    Look at that, scammers, trying to make some "cash". Whenever you get an email sent like this,
    it's scam for sure. I didn't even sign up for anything, or gave my e-mail away, and I get a
    coupld hundred thousand (in euros). I doubt it. What pure scam...I reported this to Yahoo!.
    QUOTE PAYDAY INTERNATIONAL LOTTERIA Ref : GW/06-AT/4273 Batch: GW/06/7676 We are pleased to
    inform you of the result of the just concluded annual final draws of Payday International Lotteria
    Program.Payday International Lotteria draws was conducted from an exclusive list of 2....
  29. (weird) Dreams
    Do they have a meaning? (10)
    Yesterday I've posted in the vent a reply on a topic regarding alarm clocks. And as I was typing
    my reply I started thinking that lately I dreamed a lot of weird stuff. So I said ... why not ask
    fellow members what (weird) dreams they had lately. And since we are on the subject... do you belive
    this dreams have a hidden meaning (see poll)? Before I get started... I think it would be best that
    you first type your reply (if you intend to reply) and then read other posts. Why? So that you are
    not influenced by other opinions. As I said I had a lot of weird dreams late....
  30. Dahv A Weird Girl Rapper.
    she's crazy. (2)
    Please tell me yall do not listen to this girl... http://dahv.com/ someone said her voice is
    nice. A 12 year old, white girl, trying to rap. Another Vanilla Ice. Listen to her other songs and
    tell me if you like her. lol. i think its a joke. Especially the song called School. lol. its been
    long since i visited her site. she has some new weird songs like Mean Girls. lol. The only decent
    song to me is 7,6,5,4,3,2,1 lol. omg. she still makes me crack up... classic. ewww she's with
    Radio Disney. nasti.....

    1. Looking for php, weird, calculations

Searching Video's for php, weird, calculations
advertisement



Php Is Doing Some Weird Calculations



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE