Division of doubles wrong?

Ok. I just came across this wierd thing and wasn't sure why it was happening. Basically, if I divide the number 33.333 by 11.111 I do not get 3 if either of the numbers is a double.
For instance, division of 33.333/11.111, 33.333f/11.111, 33.333/11.111f, and 33.333f/11.111f produces the following:
2.9999999999999996
2.999999983520343
3.0000000164796568
3.0
Is this just roundoff error when using doubles?

Hi vlovich,
33.333/11.111 gives as good result as you can expect since you divide double by double.
33.333f/11.111f is just a little better, but this is by chance, you are not garantied to get worse results
by using float. However the maximum possible error is larger when using floats.
In the other two cases where you mix float and double, I guess (this is whats happens in C at least) that the floats are converted to doubles. Thus you first convert the decimal reprecentation 11.111f to
float, then the float to double. In each of these conversions you lose precision.
When using float and do just one simple operation you can expect about 6-7 digits in the answer to be "correct", with double about 15. If you do many calculations, like adding 1000000 numbers these errors sums up and get bigger in the result.
Hope this helps
/David

Similar Messages

  • The names of Red Hot Chili Peppers Albums Californication and Stadium Arcadium are all doubled (Wrong names to titles)

    Could anybody (or admin) help me to solve the problem?
    best wishes
    marius

    Generally all you need to do is fill in an appropriate Album Artist. For more details see my article on Grouping Tracks Into Albums, in particular the topic One album, too many covers.
    tt2

  • Problem in creating sales order with BAPI_SALESDOCU_CREATEFROMDATA1.

    Hi everybody
    I am not able create a sales order with BAPI_SALESDOCU_CREATEFROMDATA1.i also did debugging.its not throwing  up any errors in return table. return table is empty. but when i check the  BAPI in se37 with same data its creating the sales order.
    Here i am attaching my program.please kindly can anyone have a look and tell me whether i did anything wrong in  the program.
    thx in advance.
    *& Report  YORDERCREATE
    REPORT  YORDERCREATE.
    data: v_vbeln like vbak-vbeln.
    data: header like bapisdhead1.
    data: headerx like bapisdhead1x.
    data: item like bapisditem occurs 0 with header line.
    data: itemx like bapisditemx occurs 0 with header line.
    data: partner like bapipartnr occurs 0 with header line.
    data: return like bapiret2 occurs 0 with header line.
    data: it_schedules_inx like bapischdlx occurs 0 with header line.
    data: it_schedules_in like bapischdl occurs 0 with header line.
    types: begin of itab,
    auart like vbak-auart,
    vkorg like vbak-vkorg,
    vtweg like vbak-vtweg,
    spart like vbak-spart,
    kunnr like kuagv-kunnr,
    kunnr1 like kuwev-kunnr,
    posnr like  vbup-posnr,
    matnr like mara-matnr,
    Tquan type string,
    end of itab.
    data: it-out type table of itab with header line.
    data: menge(10) type c.
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        FILENAME                      = 'C:\VA01.TXT'
      FILETYPE                      = 'ASC'
       HAS_FIELD_SEPARATOR           = 'X'
      TABLES
        DATA_TAB                      = it-out.
    loop at it-out.
    header-doc_type = it-out-auart.
    headerx-doc_type = 'X'.
    header-sales_org = it-out-vkorg.
    headerx-sales_org = 'X'.
    header-distr_chan = it-out-vtweg.
    headerx-distr_chan = 'X'.
    header-division = it-out-spart.
    headerx-division = 'x'.
    headerx-updateflag = 'I'.
    *partner data
    partner-partn_role = 'SP'.
    partner-partn_numb = it-out-kunnr.
    append partner.
    partner-partn_role = 'SH'.
    partner-partn_numb = it-out-kunnr1.
    append partner.
    *item data
    itemx-updateflag = 'I'.
    item-itm_number = it-out-posnr.
    itemx-itm_number = 'X'.
    item-material = it-out-matnr.
    itemx-material = 'X'.
    item-target_qty = it-out-tquan..
    itemx-target_qty = 'X'.
    APPEND item.
    APPEND itemx.
    *loop at it-out.
    *write:/ it-out-auart,it-out-vkorg.
    *endloop.
    CALL FUNCTION 'BAPI_SALESDOCU_CREATEFROMDATA1'
      EXPORTING
       SALESDOCUMENT                 = v_vbeln
        SALES_HEADER_IN               = header
        SALES_HEADER_INX              = headerx
      SENDER                        = SENDER
      BINARY_RELATIONSHIPTYPE       = 'VORL'
      INT_NUMBER_ASSIGNMENT         = ' '
    IMPORTING
       SALESDOCUMENT_EX              = v_vbeln
    TABLES
       RETURN                        = return
       SALES_ITEMS_IN                = item
       SALES_ITEMS_INX               = itemx
       SALES_PARTNERS                = partner.
      SALES_SCHEDULES_IN            = SALES_SCHEDULES_IN
      SALES_SCHEDULES_INX           = SALES_SCHEDULES_INX
      SALES_CONDITIONS_IN           = SALES_CONDITIONS_IN
      SALES_CFGS_REF                = SALES_CFGS_REF
      SALES_CFGS_INST               = SALES_CFGS_INST
      SALES_CFGS_PART_OF            = SALES_CFGS_PART_OF
      SALES_CFGS_VALUE              = SALES_CFGS_VALUE
      SALES_CFGS_BLOB               = SALES_CFGS_BLOB
      SALES_CCARD                   = SALES_CCARD
      KEY_TABLE                     = KEY_TABLE
    endloop.
    loop at return where type  = 'E' or type = 'A'.
    exit.
    endloop.
    if sy-subrc = 0.
    write:/ 'error in creating document'.
    else.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
       WAIT          = 'X'
    IMPORTING
      RETURN        = RETURN
    commit work and wait.
    write:/ 'document',v_vbeln,'created'.
    endif.

    Hi Patrick,
    Here are a couple of issues/recommendations:
    1) Checkboxes must be populated by capital X (your headerx-division = 'x' is wrong)
    2) Why you are using the COMMIT WORK AND WAIT statement? The CALL BAPI_TRANSACTION_COMMIT does exactly the same! Comment it out or better delete one of them!
    3) Does one of your sales order tables (VBAK, VBAP, ....) have any customer appended fields? In this case you have to populate an additional value table and the related index table as well.
    Check it out,
    Heinz

  • Math in Java

    What is wrong with Java's math. It adds double wrong sometimes. Why is this? Is there a way to hard-code it so it will add right?
    Regards,
    Collin

    And it's not a Java thing. C/C++ have it, as I'd imagine C# does, as do many other languages.
    It's a natural consequence of two things:
    1) trying to represent an infinite number of values with a finite number of bits.
    2) the fact that some things that can be represented exactly in base-10 (what we humans use) cannot be represented exactly in base-2 (what 'puters use).
    For instance 0.1 base-10 cannot be represented exactly in base-2. In base-10, 0.1 is 1/10, 0.01 is 1/100, etc. In base-2, 0.1 is 1/2, 0.01 is 1/4, 0.001 is 1/8, etc. There is no way--absolutely no way--to represent 1/10 exactly in that notation with a finite number of digits.
    http://docs.sun.com/source/806-3568/ncg_goldberg.html

  • I have a problem with my calculator :(

    hi all,
    i need some help with my calculator..after i wrote the code i discovered my mistake so i made some changes on the code..the problem is that it's still applying the old code !!
    ( i didn't forgot to compile it, & i closed all the browsers b4 trying it )
    i will appreciate ANY suggestion
    P.S : my mistake was with the operations buttons ( add, sub, multip, division ) & the equal button
    if it may help, this is the code ( the new one, then the old one )
    the new code:
    import java.awt.* ; // Container, FlowLayout
    import java.awt.event.* ; // ActionEvent, ActionListener
    import javax.swing.* ; // JApplet , JButton , JLabal, JTextField
    public class Calculator2 extends JApplet implements ActionListener {
    // graphical user interface components
    JTextField field ;
    JButton zero, one, two, three, four, five, six, seven, eight, nine, fraction, clear,
    add, sub, multip , division, equal ,sin , cos, tan ,log ,sqrt ,exp ;
    // variables
    String string = " " ; // to store what is in the text field
    String operation ; // to store the operation selected
    double operate ; // to store the result of the operation selected
    double operand1 , operand2 ; // the operands of the operation
    // set up GUI components
    public void init ()
    Container container = getContentPane ();
    container.setLayout ( new FlowLayout () );
    // create a text field
    field = new JTextField ( 17 );
    container.add ( field ) ;
    // create buttons
    clear = new JButton ( "C" ) ;
    clear.addActionListener ( this ) ;
    container.add ( clear );
    zero = new JButton ( "0" ) ;
    zero.addActionListener ( this ) ;
    container.add ( zero );
    one = new JButton ( "1" ) ;
    one.addActionListener ( this ) ;
    container.add ( one );
    two = new JButton ( "2" ) ;
    two.addActionListener ( this ) ;
    container.add ( two );
    three = new JButton ( "3" ) ;
    three.addActionListener ( this ) ;
    container.add ( three );
    four = new JButton ( "4" ) ;
    four.addActionListener ( this ) ;
    container.add ( four );
    five = new JButton ( "5" ) ;
    five.addActionListener ( this ) ;
    container.add ( five );
    six = new JButton ( "6" ) ;
    six.addActionListener ( this ) ;
    container.add ( six );
    seven = new JButton ( "7" ) ;
    seven.addActionListener ( this ) ;
    container.add ( seven );
    eight = new JButton ( "8" ) ;
    eight.addActionListener ( this ) ;
    container.add ( eight );
    nine = new JButton ( "9" ) ;
    nine.addActionListener ( this ) ;
    container.add ( nine );
    fraction = new JButton ( "." ) ;
    fraction.addActionListener ( this ) ;
    container.add ( fraction );
    add = new JButton ( "+" ) ;
    add.addActionListener ( this ) ;
    container.add ( add );
    sub = new JButton ( "-" ) ;
    sub.addActionListener ( this ) ;
    container.add ( sub );
    multip = new JButton ( "*" ) ;
    multip.addActionListener ( this ) ;
    container.add ( multip );
    division = new JButton ( "�" ) ;
    division.addActionListener ( this ) ;
    container.add ( division );
    sin = new JButton ( "sin" ) ;
    sin.addActionListener ( this ) ;
    container.add ( sin );
    cos = new JButton ( "cos" ) ;
    cos.addActionListener ( this ) ;
    container.add ( cos );
    tan = new JButton ( "tan" ) ;
    tan.addActionListener ( this ) ;
    container.add ( tan );
    log = new JButton ( "log" ) ;
    log.addActionListener ( this ) ;
    container.add ( log );
    sqrt = new JButton ( "sqrt" ) ;
    sqrt.addActionListener ( this ) ;
    container.add ( sqrt );
    exp = new JButton ( "exp" ) ;
    exp.addActionListener ( this ) ;
    container.add ( exp );
    equal = new JButton ( "=" ) ;
    equal.addActionListener ( this ) ;
    container.add ( equal );
    } // end of method init
    public void actionPerformed ( ActionEvent event )
         // button zero
         if ( event.getSource()== zero )
         string = string + "0" ;
         field.setText ( string ) ;
         // button one
         else if ( event.getSource()== one )
              string = string + "1" ;
              field.setText ( string ) ;
         // button two
         else if ( event.getSource()== two )
              string = string + "2" ;
              field.setText ( string ) ;
         // button three
         else if ( event.getSource()== three )
              string = string + "3" ;
              field.setText ( string ) ;
         // button four
         else if ( event.getSource()== four )
              string = string + "4" ;
              field.setText ( string ) ;
         // button five
         else if ( event.getSource()== five )
              string = string + "5" ;
              field.setText ( string ) ;
         // button six
         else if ( event.getSource()== six )
              string = string + "6" ;
              field.setText ( string ) ;
         // button seven
         else if ( event.getSource()== seven )
              string = string + "7" ;
              field.setText ( string ) ;
         // button eight
         else if ( event.getSource()== eight )
              string = string + "8" ;
              field.setText ( string ) ;
         // button nine
         else if ( event.getSource()== nine )
              string = string + "9" ;
              field.setText ( string ) ;
         // button fraction
         else if ( event.getSource()== fraction )
              string = string + "." ;
              field.setText ( string ) ;
         // button clear
         else if ( event.getSource()== clear )
              clear ();
         // button add
         else if ( event.getSource()== add )
              operand1 = Double.parseDouble ( string );
              operation = "+" ;
              clear ();
         // button sub
         else if ( event.getSource()== sub )
              operand1 = Double.parseDouble ( string );
              operation = "-" ;
              clear ();
         // button multip
         else if ( event.getSource()== multip )
              operand1 = Double.parseDouble ( string );
              operation = "*" ;
              clear ();
         // button division
         else if ( event.getSource()== division )
              operand1 = Double.parseDouble ( string );
              operation = "/" ;
              clear ();
         // button sin
         else if ( event.getSource()== sin )
              operate = Double.parseDouble ( string ) ;
              operate = Math.sin( operate );
              field.setText ( Double.toString ( operate ) ) ;
    // button cos
         else if ( event.getSource()== cos )
              operate = Double.parseDouble ( string ) ;
              operate = Math.cos( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button tan
         else if ( event.getSource()== tan )
              operate = Double.parseDouble ( string ) ;
              operate = Math.tan( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button log
         else if ( event.getSource()== log )
              operate = Double.parseDouble ( string ) ;
              operate = Math.log( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button sqrt
         else if ( event.getSource()== sqrt )
              operate = Double.parseDouble ( string ) ;
              operate = Math.sqrt( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button exp
         else if ( event.getSource()== exp )
              operate = Double.parseDouble ( string ) ;
              operate = Math.exp( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button equal
         else // if ( event.getSource()== equal )
              operand2 = Double.parseDouble ( string );
              if ( operation == "+" )
                   operate = operand1 + operand2 ;
              else if ( operation == "-" )
                   operate = operand1 - operand2 ;
              else if ( operation == "*" )
                   operate = operand1 * operand2 ;
              else if ( operation == "/" )
                   operate = operand1 / operand2 ;
              field.setText ( Double.toString ( operate ) ) ;
    } // end of method actionPerformed
    public void clear ()
         string = " ";
         field.setText ( string ) ;
    } // end of method clear
    } // end of class
    the old code which have the problem ( which i make the changes on ) :
         // button clear
         else if ( event.getSource()== clear )
              string = "";
              field.setText ( string ) ;
         // button add
         else if ( event.getSource()== add )
              string = string + "+" ;
              field.setText ( string ) ;
         // button sub
         else if ( event.getSource()== sub )
              string = string + "-" ;
              field.setText ( string ) ;
         // button multip
         else if ( event.getSource()== multip )
              string = string + "*" ;
              field.setText ( string ) ;
         // button division
         else if ( event.getSource()== division )
              string = string + "/" ;
              field.setText ( string ) ;
         // button equal
         else // if ( event.getSource()== equal )
              operate = Double.parseDouble ( string ) ;
              field.setText ( Double.toString ( operate ) ) ;
    thanks a lot :)

    Open Java console and press "x" (Clear cache)
    Disable caching in java paremeters.

  • User Exit Fixed Point Arithmetic

    Dear all,
      I have developed some code on the user exit RV61AFZB and tried to change the value on the xkomv-kbetr.  But, i found that the division result is wrong.
    Example:
    DATA: temp like xkomv-kbetr,
               c_test LIKE xkomv-kbetr VALUE '152000.00',
               c_qty like xkomv-kbetr value '3.00'.
       temp  = c_test / c_qty.
    The value of temp became 506.67.  Since, this is a user exit and i can't change program attribute "Fixed Point Arithmetic" .
    Regards,
    Kit

    Hello
    Try this:
    DATA: temp like xkomv-kbetr,
               c_test LIKE xkomv-kbetr VALUE '152000.00',
               c_qty like xkomv-kbetr value '3.00'.
    data: temp1 type i.
       temp  = c_test / c_qty.
       temp1 = temp.

  • Batch Printing of Invoice VF31

    Hi All,
    i am printing invoices in Batch ie. VF31.
    Few invoices of particular division are printed wrongly
    ie. some conditions with values of previous invoice are also printed on current invoice(on current invoice its correct values are also printed)
    any configuration is missing?my abaper is saying some config. is missing.
    Reg.
    Amol

    Hi,
    i got the same error in my last project..
    wt i did .. i created a scenarion in dev client means i took 15 invoices list and started getting output in vf31..
    finally i got the same issue there also..
    then i asked to My abaper to debug the error..
    finally we come to know that was due to optisuite..
    there was overlapping of data there in optisuite itself...
    then technical guy did miror changes.. eventually i got my solution..
    hope u also follow the same.. i agree this is time taking and boaring job to take bunch of output.. bt this will keep coming ..so etter to prevent repition..
    thanks
    mahesh

  • My phone is in some mode where it will say everything i am trying to open and i have to double tap on it to open and wont scroll. whats wrong?

    my phone is in some mode where it will say everything i am trying to open and i have to double tap on it to open and wont scroll. whats wrong?

    You are in VoiceOver mode, you need to go into your settings>general>accessibility and turn off VoiceOver. 

  • Bug with double division ??

    Hello,
    first sorry my bad english...
    I am trying to convert a C program in java and i had a big problem with double division.
    For example, my program needs to do that :
    double d = 2. * 3.1415926 ;
    double d2=511;
    double d3=d/d2;
    The value in memory of d3 is 0x3f892e92965e074e whereas with my original C program, for the same code) the value in memory is 0x3f892e92965e074d !!!!!! (the last byte is different)
    So what is the problem ? Is the double division in java different with the C language or is it my code which has a problem ??
    Thanks a lot

    It could be different than (some implementation of) C, but that doesn't mean it's a bug or a problem. Java is supposed to implement an IEEE floating point specification; if you can demonstrate that it's not following that spec (I think it's IEEE 754) then report it as a bug. Incidentally, do Java and your version of C implement 3.1415926 the same way?
    (Also, if that number is supposed to be pi, then the inaccuracy you claim in Java is much less than the inaccuracy you made by using an approximation to pi.)

  • Ok so I downloaded the iOS6 and now my iPod is reading my day and time and everything else to me I have to double tap my numbers to get in my password but my password won't even work can someone please tell me what's wrong

    So I downloaded the iOS6 on my iPod 4 and after 2 days it starts to read me everything I mean from my time and date to my password I have to double tap in my password In order for numbers to show up but when I type them it just says it's wrong please help what can I do to fix this problem with out having to reset EVERYTHING

    Triple Click your home button to turn off VoiceOver, or go to Settings > General > Accessibility and turn VoiceOver off.
    https://www.apple.com/accessibility/ios/voiceover/

  • Trouble with calculating fields. Can't select (check) fields. Also can't figure out what's wrong with a division field (percent) that I created. Keep getting the pop up that format of the field doesn't allow blah blah blah... Help!

    Trouble with calculating fields. Can't select (check) fields. Also can't figure out what's wrong with a division field (percent) that I created. Keep getting the pop up that format of the field doesn't allow blah blah blah... Help!

    1. Use the mouse to select the field and then press the space bar.
    2. A null string is the same as zero. What is the result for division by zero?

  • When I double click run installation, then proceed to restart, I restart the computer,  but when it loads back up, I get an error message, saying I need to restart, whats wrong?

    When I double click run installation, then proceed to restart, I restart the computer,  but when it loads back up, I get an error message, saying I need to restart, whats wrong?

    I get an error message, saying I need to restart, whats wrong?
    That is a "kernel panic".
    Try here.  Resolving Kernel Panics

  • SPListItemCollection.GetDataTable() delivers wrong Double Values

    Hi there,
    i have a Problem with the .getDataTable() Method of a SPListItemCollection Object. Within the SPListItemCollection all Double Values are displayed correct. But the DataTable Edition puts the decimal point to the End of the number. And i have no idea why,
    and how to get rid of this. Any hints?
    DataTable dt = new DataTable("ChargeData");
    listItems[0]["PRICE"] -> 107.10 | object {double}
    dt = listItems.GetDataTable();
    dt[0]["PRICE"] -> 10071.0 | object {double}
    P.s.: If this is realted in some way, the initial List for the SPListItemCollection is a External List with a MSSQL Connection. The 'Price' Column within the SQL Server is a "float".
    My Best Regards
    THO
    EDIT #1
    I've try to figure out when this problem appears. Always or only on BCS lists, so i tried (with Powershell), and found out, this seems to be a Problem with the Autogenerated BCS List.
    $nonbcslist.Items[0]["PRICE"] -> 107,10 | double
    $dt = $nonbcslist.Items.GetDataTable()
    $dt.Rows[0]["PRICE"] -> 107,10 | double
    $bcslist.Items[0]["PRICE"] -> 107,10 | double
    $bcsdt = $bcslist.Items.GetDataTable()
    $bcsdt.Rows[0]["PRICE"] -> 10710 | double

    I noticed that in one example you are using a period to segment the double and in the other a comma. I wonder if that is somehow tied into the wrong conversion when returning a data table.
    I have seen GetDataTable() break some other formatting in the past, specifically when working with multi-select user fields. Your best bet may be to iterate through the collection and manually populate a blank table.
    Dimitri Ayrapetov (MCSE: SharePoint)

  • Photoshop CS 6 - image -canvas - make double matt - select color - if I select wrong color, how do I change to a new color, right now it won't change to the new color I select, WHY?????

    Photoshop CS 6 - image -canvas - make double matte - select color - if I select wrong color, how do I change to a new color, right now it won't change to the new color I select, WHY?????

    Hi, The options bar shows sample size 101 by 101 Average. That was my problem. When I was troubleshooting in order to try to explain to you what was happening, I noticed that when I sampled my models teeth, it gave me a color a bit lighter than her lipstick as my foreground color in my tool box. That started me thinking in the correct way. I didn't realize that my sample size had somehow gotten switched from point sample to 101 by 101 average. I appreciate the help. Thank you.

  • Compare of double after division

    HI!
    Plese see attached VI. I divide two doubles and compare the result with another double. This works for low numbers but if the result of the division is for example 1000.00 (4.5/4.5e-3)the greater as function (compared with 1000.00) provides true at the output. Has it something to do with the internal rounding, representation of the doubles? Or how LV performs the division?
    How can I get the right response from the greater as function?
    ANDY
    Attachments:
    greater.vi ‏20 KB

    It is the conversion of floating point numbers to binary representation that computers understand and the problem is present in all programming languages. Comparison of floating point numbers is something that should be avoided. There is a discussion of this on the LabVIEW FAQ. Select the Front Panel subforum and the "Why does my output show -0.99 .." and "So how do I .." topics.

Maybe you are looking for