Adding two double numbers is a problem

Hi friends..
when we try to add the two double numbers say,
119.52, 10.00
here we should get 129.52
but it is giving as 129.51999999999998
Here i want to round this to two digits after decimal point
if i round the above value i will get 129.52 and the problem will be solved.
But we don't know exactly on what basis we are getting the sum as 129.51999999999998.
Assume that, tomorrow when we are adding some other numbers we may got like
129.51444444444448
when we round this we get 129.51
but actually we have to get 129.52.
If anyone know why the system is giving that wrong sum amount, please give solution to avoid this.
To solve this problem, i tried by converting the double numbers to integers and adding these integers and finally converting the sum into double...like
if
d1= 119.52
d2=10
int x = (int)(119.52*100.00);
int y = (int)(10.00*100.00);
int z = x + y;
double d = (double)(z)/100;
This is working for this case...
But when we are applying this approch to other numbers it is giving problem....
i.e, if i convert like below
int x = (int)(72.46*100.00);
this should give x=7246
but it is giving x=7245
What is the solution for this problem...please give immediate reply.....

Assume that, tomorrow when we are adding some other
numbers we may got like129.51444444444448
That isn't going to happen. The numerical computations are deterministic. The problem lies in your understanding of how numbers work.
This has been repeated before so if you want more detail I suggest you search the forums for discussion on precision and double.
Computers store numbers in binary. The number you are trying to represent is a decimal number. This produces an error in precision.
The same problem exists in decimal notation. Decimal notation can not accurately represent one divided by three (in decimal 0.33333333.....).

Similar Messages

  • Adding two double's together??

    Hi,
    I want to add two prices which are Strings to start off with and to get the end calculation in J2ME.
    As I can't parse the string into a double, how do I go about doing all of this (String -> double -> end result which is the addition fo the two prices?
    i.e. �12.50 + �13.50 = calculatedResult ??
    Any help would be appreciated,
    Harry

    Hi again,
    I have another problem. If a value comes in for example at �1.995 and say is added to a value �0.005, how can this be dealt with when rounding up? This needs to go to �2 but using the code you provided will only get me to �10.95.
    I have to handle cases where the value may come at �1.995, �1.996, �1.997, �1.998 and �1.999. Each of these 5 values will be added to �0.005 and I really need the addition of the 2 values to be rounded properly. i.e. if �1.996 is added to �0.005 then the resultant answer is �2.00 and not �2.001. The last case �1.999 + �0.005 will come to �2.004 so the half pence will never become a full pence if you understand what I'm saying...? But �9.999 + �0.005 will become �10.004 so the pound has changed here and will need to be dealt with.
    Please help!

  • Adding two possitive numbers results a negative????

    Hello everybody i posted that in an other subforum but i guess it was the wrong place. I have a strange problem that it's racking my brain! i have the following code for (int i=0;i<ascii.length;i++){
    int randNum=rand.nextInt(100000000);
    buffer=randNum*256+ascii[i];
    (ascii[i] is an array of four integers that picture an ascii characters).
    Normaly that would result a buffer full of positive numbers.In my case thats not always true. Many times i get as a result a negative number even if the other two numbers that i add are positive. What's wrong here?? Thanks in advance!

    Integer.MAX_VALUE is 0111 1111 1111 1111 1111 1111 1111 1111
    Add one to it, and you wrap around to Integer.MIN_VALUE: 1000 0000 000 0000 0000 0000 0000 0000 (-2 billionish)
    Keep addiing, and you end up at -1: 1111 1111 1111 1111 1111 1111 1111 1111
    http://en.wikipedia.org/wiki/Twos_complement

  • Adding two special double numbers

    Adding two double numbers.
    say, 12543.34 and 42895.00 am getting in the decimal part .3999999.
    Now I want .34 instead .399999 and how??
    Can any body help me ??

    Read this (or search the forums--this question is asked at least once a day):
    http://docs.sun.com/source/806-3568/ncg_goldberg.html

  • Problem with the  addition of  double numbers

    when we try to add the two double numbers say,
    119.52, 10.00
    here we should get 129.52
    but it is giving as 129.51999999999998
    This is happening only for sum numbers.
    Here i want to round this to two digits after decimal point
    if i round the above value i will get 129.52 and the problem will be solved.
    But we don't know exactly on what basis we are getting the sum as 129.51999999999998.
    Assume that, tomorrow when we are adding some other numbers we may got like
    129.51444444444448
    when we round this we get 129.51
    but actually we have to get 129.52.
    If anyone know why the system is giving that wrong sum , please give solution to avoid this.
    In My application , i want the exact sum amount so if i add some numbers i should get exact sum.
    like 119.52+10=129.52
    i want exactly this. How to get this.

    As another poster said, this is a topic worth reading up on, as there are subtleties involved in this kind of imprecise math. A couple of points to get you started though...
    * Java's double type has a precision of something like 16 (20? 24?) decimal places. (You should be able to find the correct number in the lang. spec., or someone may be kind enough to post it here.) I don't remember the formal definition, but what this means, approximately, is that any number that can be represented will be accurate to within +/-0.5*10^-16 of its value. That is, if the actual value is 2e5, then the absolute value of the error in the representation will be less than 0.5e-11. (I might be off be a factor of 2 and/or an order of magnitude here, but you get the general idea.) The good news is, no value will be in error by more than that amount. The bad news is, any value could be in error by up to that amount. These are the two key points.
    * Compare the precision implicit in your data and needed in your results to that of Java, along with the number of intermediate calculations you'll do to get a final result. For instance, if you're doing financial calcs in the tens of billions of dollars, and you need accuracy to the penny, that's one part in 10^-12. Assuming any given value is off by no more than one part in 10^-16, and assuming all errors are in the same direction, if you do 10^4 cumulative additions, you'll be off by a penny. Again, these are rough, and I may have missed something, but this is the kind of thing to look at to determine how the inherent imprecision will affect you.
    * Don't round any intermediate results. Keep the precision you have. However, when you're comparing (as jschell demonstrated) make a copy of the value and round that copy before doing the comparison. Use the first two points above to determine whether that rounding will meet your needs for precision. Same goes for displaying.
    * Finally, if the 10^-16 (or whatever it was) precision of a double is not sufficient, you can get arbitrary precision with BigDecimal. There are a couple of caveats, however. 1) It's a lot slower than using primitives ans 2) Arbitrary precision does not mean infinite precision. You can specify as many decimal places as you want (subject to time and memory constraints), but you even if you specify 1,000 decimal places, you can still be off by 5 in the 1001st place.

  • Problme while adding double numbers.

    Hi,
    I am trying to get a series of double numbers by adding some constant value to a double number.
    so when i try to add .01 to 10.04 i am getting the result as 10.0499999.. instead of 10.05
    is there any work around for this problem.
    Thanks

    Can Someone tell me please what is going on?
    Trying to add simple double numbers but the result is really surprising.
    double postage = 0.72 ;
    double total = 0.18;
    double totalamt = postage + total;
    System.out.println(totalamt);
    Result: 0.8999999999999999 INSTEAD OF 0.9

  • Problem araised in  adding two custom fields to ksb1 tcode.

    hi experts,
      I added tow fields vendor no, vendor name to Tcode:KSB1.
    i used user-exit :   coomep01 .
    in this exit i added two fields to ci_rkpos (include table) . i.e zlifnr,zname1.
    but that problem is ci_rkpos is not appearing in green color it means it is not transported .
    in development  client is working properly .
    using sm34 i added fields names.
    error is coming like this.
    The following syntax error occurred in the program SAPLXKAEP :
    "The data object "CS_RECORD" has no component called "ZLIFNR", but ther"
    Error in ABAP application program.
    The current ABAP program "SAPLKAEP" had to be terminated because one of the
    here wat ever we add the zfields will appear in kaep_coac ,so in this structure two added fields are appeared in dev client
    but,the same zfields when transported to quality is not appearing .
    please suggest how to do??

    hi breakpoint.
    yes i attached  in transport.
    in quality client.
    wat ever i wriiten code  in user exit it is coming ,but
    incldue table structure is not coming ,and even that fields are appearing in v_tkalv ,but in structure kaep_coac
    blank include strcuture CI_RKPOS is appearing ,but
    in dev client is working properly, and in quality first of all report is going to dump.
    here this tvode is using all 3 plants,how can i restrict to specified plant so that ,it will not affect others plant.
    suggest me how to do??
    Edited by: kamalsac on Aug 18, 2010 10:57 AM

  • I just added two external firewire drives and they keep going to sleep.  I did not have this problem when USB.  I have turned off "put disks to sleep" in power preference.

    I just added two external firewire drives and they keep going to sleep.  I did not have this problem when using USB external drives.  I have turned off "put disks to sleep" in power preference.

    It is not clear if these are the same drives, just connected
    differently, or different drives?
    One thing that may be happening is that the drives firmware
    itself is putting it to sleep when inactive.  Many drives on
    the market have ths feature and it is turned on by default
    and require a drive utility from the manufacturer to disable it.

  • I am having problems because two phone numbers are on the Apple ID, how do I choose one for imessage?

    I am having problems because two phone numbers are on the Apple ID, how do I choose one for imessage?

    Hello,
    In my opinion, this is the best procedure to use to update your BB OS:
    http://supportforums.blackberry.com/t5/BlackBerry-Device-Software/How-To-Reload-Your-Operating-Syste...
    However, the official download site:
    http://us.blackberry.com/support/downloads/download_sites.jsp
    shows your installed OS to be the current version from your carrier. Therefore, how your friends got a newer OS is a question to ask them and/or your carrier.
    You are, of course, free to use (via the above procedure) any carriers OS package...if another has the newer OS you want, you can use it. If you choose to do so, then simply insert, between steps 1 and 2 in the above procedure, the deletion, on your PC, of a file named VENDOR.XML
    Good luck.
    Occam's Razor nearly always applies when troubleshooting technology issues!
    If anyone has been helpful to you, please show your appreciation by clicking the button inside of their post. Please click here and read, along with the threads to which it links, for helpful information to guide you as you proceed. I always recommend that you treat your BlackBerry like any other computing device, including using a regular backup schedule...click here for an article with instructions.
    Join our BBM Channels
    BSCF General Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • I accidentally added two .doc files, they show in iTunes file sharing, but don't show on iPad?

    I accidentally added two .doc files by dragging and dropping into itunes, under my device, then the app tab, then file sharing for Adobe Reader, they show in iTunes file sharing window, but don't show on iPad 3, and I cannot delete them on iTunes screen? Please help!

    First, I would advise you to apply Tao philosophy to this. Given the dysfunction of most software, if this was just an accidental copy and you don't want them on iPad and they are not showing up anyway, what's the problem? Forget it. It's not worth the hassles. I'm more concerned that you have psychological issues you need to be dealing with, not this. You are making much too unnecessary work and stress for yourself.
    As for deleting them, I'm assuming you highlighted them and hit delete and it didn't work. If not, it should. Works for me. But if it doesn't, my advice is also not to worry about it, as annoying as the clutter is. It will eventually resolve itself (like when your data files corrupt or your iPad crashes, LOL. (I apologize for my cynicism but I am fed up with dysfunctional software). It's not worth the hassles of trying to figure out petty issues.
    As for me, I have the opposite, and very serious problem. I've got Reader on my iPad too. But I can't move the pdfs out of it and onto my laptop with iTunes' dysfunctional file sharing. I get the error "file cannot be copied because you do not have permission to see its contents." I have hundreds of pdfs on my iPad that I converted from web surfing with other programs that also don't work and I can't get them off the iPad for backup and to have a synced library I can access from both laptop and iPad.
    I called iTunes twice and both times they blamed it on the app, saying iTunes file sharing doesn't support third party apps and that I should call Adobe and talk them into writing the code that will work with iTunes. Yeah, right. And Reader is not the only problem. This is happening with iAnnotate and Write PDF and other apps.
    I suspect that both of us have the root problem, perhaps Apple's greedy refusal to support the apps that it sells in the app store and are quasi-integrated enough to show up in the file sharing documents list, but can't be moved or deleted; and the apps that don't bother to make their apps fully work with iTunes file sharing.
    If anyone has any solutions ......

  • Numbers 08 Zoom Problem

    I did a clean install of Mountain Lion and then reinstalled iWork 08. All the programs are functioning normally except numbers. The zoom settings are now set as: 2500%, 5000%, 7500%, 10000% etc... rather than 25%, 50%... The zoom setting in the prefernce pane shows the correct setting (ie 25%, 50%) but  on the the worksheet everything remains unchanged at 2500% etc. Any suggestions?

    Just in case I didn't explain myself clearly here are two screen shots of the problem:
    The Default Zoom in the preferences pane:
    and what shows up on the spreadsheet:

  • How can I get two missing numbers on a iTunes card

    How can I get two missing numbers on a iTunes card

    "Get help",just below the form space where I entered the code. Once I clicked "Get Help", it added another line to the form where I could enter  THE NUMBER ON THE BOTTOM LEFT CORNER on the back of the card. I entered as much of the I scratch-off code I could read, along with the serial number.

  • I have duplicate pictures in "my catalog" that I believe come from two Adobe folders found under "my pictures/adobe/ one is Revel and the other is Photoshop express which preceded revel. Can I delete those two folder without causing a problem with my cata

    I have duplicate pictures in "my catalog" that I believe come from two Adobe folders found under "my pictures/adobe/ one is Revel and the other is Photoshop express which preceded revel. Can I delete those two folder without causing a problem with my catalog?

    Thanks for the links, Limnos.
    If you are willing to continue helping, here's what I found.
    Just to clarify the two iTunes folders I am refering to are:
    username-->Music-->iTunes
    HD-->iTunes
    I am presuming each location has a full set of files as outlined in the above links?
    Not all the files are in both locations. Most are.
    - The Itunes folder in my home folder does not have itunes library.xml.
    - The Itunes folder in my home folder has a subfolder called Mobile Applications (username-->Music-->iTunes--> Mobile application). The Itunes folder at the HD level also has a Mobile Application folder but it is a subfolder of Itunes Media folder (HD-->iTunes--> iTunes Media-->Mobile applications) and has no files in it.
    - I do not have an iTunes Media in the iTunes folder in my home folder.
    - also the Itunes media folder (HD-->iTunes--> iTunes Media) has subfolders by type (books, movies, itunes u, music etc...) but the iTunes Media-->Music also has some of the same subfolders ( iTunes Media-->Music-->books, iTunes Media-->Music-->Movies, iTunes Media-->Music-->iTunes U). Is this normal repetition?
    You say:
    /itunes/itunes media/ music
    but it is important to note what comes before all that.
    There is nothing as far as I can tell before that first forward slash. Since the only iTunes Media folder I have is in the iTunes folder that resides at the HD level (HD-->iTunes--> iTunes Media folder) not the iTunes folder in my home folder (username-->Music-->iTunes) , I assume that's the one that holds the music.
    Keep iTunes media folder organized and Copy files to iTunes Media folder when adding to library are both checked on
    Does that give more clarity into my problem?

  • Error adding up 5 numbers! (or is it me?!)

    Hi -
    I've set Numbers to add up a column of 5 numbers and put the total in a different cell. But it's adding up the numbers wrongly. Can anyone tell me what I'm doing wrong?
    I've put a screenshot on my Public iDisk:
    https://public.me.com/glovepuppet/
    (It's the .jpg called NumbersMOZ001)
    The formula is shown as SUM(H2:H8) which makes 556.02, so why has it put the total as 556.00?
    (I know it's only 2p, but it's irritating!)
    Help please!
    Andy

    This behavior was described many many times.
    The values which you sum are the result of calculations.
    I guess that you use =my_formula
    which may return values with more than two decimal digits.
    When you ask a cell to display two decimal digits, it displays the rounded value *_but, happily, it doesn't change the stored value_*
    So you aren't summing what you see but what is stored.
    Here is your error.
    To sum what you see, you must design formulas whose result is what you see.
    =ROUND(my_formula,2) is what you need.
    So, to be short,
    Numbers did perfectly what you urged it to do.
    Next time, before writing formulas remember that *_computers are just efficient idiots_*.
    Yvan KOENIG (VALLAURIS, France) vendredi 26 novembre 2010 17:26:43

  • Adding 2 doubles together yields incorrect results

    I have a problem with adding 2 doubles to produce a running total where it appears to also add 0.0000000000000(1 to 9).
    The code illustrates my problem and is causing a big issue, I have found references to fix this which use BigDecimal but I want to now what is going wrong.
    public class Test1
    public static void main(String args[])
    String strValue = "";
    double dTotal = 0;
    double dTotalOLD = 0;
    dTotalOLD = dTotal;
    strValue = "0.0252154";
    dTotal = dTotal + (Double.parseDouble((String)strValue));
    System.out.println(dTotalOLD + " + " + strValue + " = " + dTotal);
    dTotalOLD = dTotal;
    strValue = "0.0250356";
    dTotal = dTotal + (Double.parseDouble((String)strValue));
    System.out.println(dTotalOLD + " + " + strValue + " = " + dTotal);
    System.out.println("dTotal = " + dTotal);
    Actual Results =
    0.0 + 0.0252154 = 0.0252154
    0.0252154 + 0.0250356 = 0.050251000000000004
    dTotal = 0.050251000000000004
    Expected Results =
    0.0 + 0.0252154 = 0.0252154
    0.0252154 + 0.0250356 = 0.050251
    dTotal = 0.050251
    In the main application I upload a file and sum the items for validation but most of the time the total is not what I expect.
    What am I doing wrong?
    Mark

    If you really absolutely need exact results, use BigDecimals.
    With doubles, not even 0.1 is exactly 1/10. The IEEE double value closest to 0.1 is approximately 0.1+5.5511*10^-18 -- there is a slight, but significant difference. In calculations these "representation errors" along with others accumulate and eventually can give you relative errors of significant magnitude, although most of the time they stay under 10^-14, 10^-15

Maybe you are looking for

  • Windows Mobile and syncing with Vista

    I just recently bought a new laptop (within the last week) with Vista as the operating system.  I also use Outlook 2007.  When I hook my phone up by the usb cable sometimes the computer acknowledges my phone and sometimes not.  However, when it does

  • X100e win xp pro usb installation problem

    Hello to all, i am new here and sorry for not not introducing myself properly bu this is urgent situation. Few days ago i bought Lenovo x100e Netbook Product ID: 35086AG Athlon MV40(1.6GHz), 1GB RAM, 160GB 5400rpm HD, 11.6in 1366x768 LCD, ATI Radeon

  • Reporting Services 2012 mobile support

    Hi, Does anyone have info on mobile support for Reporting Services 2012. We've found http://blogs.msdn.com/b/sqlrsteamblog/archive/2011/10/13/power-view-pass-and-mobile.aspx which is a year old. It states the direction/strategy for "web based BI solu

  • EOIO with dynamic queuename possible in SOAP sender ?

    The customer has a requirement to pass a queuename parameter within its SOAP message, and our PI SOAP sender adapter (which must use QoS EOIO) should use that queuename. Apart from the question if that makes sense: is that possible at all ? If yet, w

  • How to stop unwanted photos from being imported to Lightroom Mobile Android

    Hi, I am a subscriber to the Adobe Photo package (LR and PS). I have started using LR mobile recently on my Android phone. I found that if I turn on Auto Import for a collection, it import more than just pictures taken by the camera. Any pictures tha