Results 1 to 6 of 6

Thread: Output and Math Precision

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Output and Math Precision

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim d As Double
    5.     d = CDbl("999999999999999.01")
    6.     d = d - CDbl("999999999999999")
    7.     MsgBox d
    8.     MsgBox 0.0000000001
    9.     MsgBox CDbl("99999999999999999999999")
    10.     MsgBox Sin(CDbl("99999999999999999999999"))
    11. End Sub
    Why does the first msgbox not output with any precision
    when the second does?
    Sin takes a double as an argument, so why does the last
    statement give an error?

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Code:
    #include <iostream.h>
    #include <math.h>
    
    void main(void){
    	double d;
    	d = 999999999999999.01;
    	d -= 999999999999999;
    	cout << d << endl;
    	cout <<  0.0000000001 << endl;
        cout << (double)99999999999999999999999;
        cout << sin(99999999999999999999999);
    }
    Won't compile.

  3. #3
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    From MSDN

    Invalid procedure call or argument (Error 5)
    Anargument probably exceeds the range of permitted values. For example, the Sin function can only accept values within a certain range. Positive arguments less than 2,147,483,648 are accepted, while 2,147,483,648 generates this error.
    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    That explains it.
    What about the loss of precision?

  5. #5
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    What about the loss of precision?
    I have no idea, sorry.

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by wey97
    What about the loss of precision?
    Sometimes u can work around the inprecision by reorganising the calculation. It would be difficult to do for a large number of complicated transactions.
    eg
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim d As Double
    3.     Dim e As Double
    4.     Dim x As Integer
    5.     Dim Mydouble As Double
    6.    
    7.     For x = 1 To 10
    8.         Mydouble = Mydouble + 9 ^ x
    9.        
    10.         'This works exactly
    11.         d = Mydouble + 1
    12.         d = (d - Mydouble) / 10
    13.         MsgBox d
    14.    
    15.         'while this doesnt
    16.         d = Mydouble + 0.01
    17.         d = d - Mydouble
    18.         MsgBox d
    19.     Next
    20. End Sub
    regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width