Trying to create a vector using a structure definition as the basis in VC++ - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 38 of 38

Thread: Trying to create a vector using a structure definition as the basis in VC++

  1. #31
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    I found:

    http://msdn.microsoft.com/en-us/libr...05(VS.85).aspx

    in the Visual Programming forum sponsored by Microsoft.

    It appears that the errors that may be induced by writing files are already handled by message boxes produced by the Windows API.

    If I'm not in the midst of a function, and don't need to break out, which is the case now that I've modified the code to make it something I can now render more useful in a computational sense, wouldn't Windows handle any errors with its own, innately programmed requirement for a message box if an error occurs while writing a file, and leave me to simply ignore the error returned where the function to write the file is originally called? (I hate to pass the problem off to Windows, but perhaps that isn't a bad thing, given that it is the OS and the OS' job is writing files.) I'll simply leave a "comment" noting this decision at the point at which the error value would be returned just after the file writing function is called.

    =2kaud;2102979
    The easiest way in Microsoft Windows to output an error message in a window on the screen is to use MessageBox. The MessageBox function creates, displays, and operates a message box. The message box contains an application-defined message and title, plus any combination of predefined icons and push buttons.

    int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

    For a simple error message, uType can be MB_OK and hWnd can be NULL. Thus an example would be

    MessageBox(NULL, "Body of message", "Title", MB_OK);

    You'll need to include windows.h for this (if it's not already included in stdafx.h). You said that you are using managed c++. I have no experience of managed c++ and implications of using MessageBox in that context.

    Have more fun!
    Thank you for your kind response.
    Last edited by Protocol; January 27th, 2013 at 06:02 PM.

  2. #32
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    I substantially have my little program written, and it is generating a data file. I have to question the choice of variable types, because the value that is being tracked doesn't seem to change, although one would expect that it should, because a negative value is added to it in every step of the program. Have I somehow "desensitized" the result to the negative value being added because I'm storing the result in the wrong data type, and it simply doesn't have the precision required to reflect the small change in each step over thousands of steps? (I'm also wondering why I had to resort to the "pow(a,b)" approach to creating an exponent (i.e., "a * pow(10,b)" instead of "a * 10^b"). The code that performs the computation and calls the file generating routine follows, should anyone care to take a look.

    Thank you.

    Code:
    //BEGINNING OF CODE WRITTEN TO IMPLEMENT COMPUTATION
    //variable declaration and initiation.
    int counter=0,year=0,retval=0,count=0,FileERR;
    double delta_t = 1000000.;
    double R_uni = 8.98*pow(10.,39.);
    double delta_R = 0.;
    double G = 6.67384*pow(10.,-11.);
    double mass_uni = pow(10.,53.);
    double rho;
    //create a structured data variable
    //containing radius and year for simulation,
    //with years measured since "big bang"
        SizeVector Universe;
    	Universe.resize(7);
        char const NameFile[]="D:\\Friedout.csv";
    		for ( counter = 15000 ; counter >= 100 ; counter-- )
    		{
    		//Begin implementation of Gaussian approximation to expansion
    		//based upon cosmological constant as dominant energy form.
    			rho = 7/3*mass_uni/(4/3*3.1415*pow(R_uni,3));
    			delta_R = -2*R_uni*(sqrt(2*3.1415*G*rho/3))*delta_t;
    			R_uni = R_uni + delta_R;
    		//End Gaussian approximation to expansion
    			if (counter==100||counter==2500||counter==5000||counter==7500||counter==10000||counter==12500||counter==15000)
    			{
    			//This segment merely selects the values to be saved in the output file
    			//in a manner to be consistent with that used to produce spreadsheet
    			//graphs via an alternate model.
    			Universe[count].year =double(counter);
    			Universe[count].R = double(R_uni);
    			count = count++;
    			}
    		}
    		retval=write_to_file(count, Universe, NameFile);
    
    		if (retval ==-1) 
    				{
    				
    				}
    
    
    
    
    
    
    
    
    //END OF CODE TO COMPUTE VALUES AND CALL FILE STORAGE ROUTINE

  3. #33
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,996

    Re: Trying to create a vector using a structure definition as the basis in VC++

    A couple of points.

    1) c/c++ does not have an in-built 'power' operator. The '^' operator is actually the xor bitwise operator. Hence the pow function needs to be used.

    2) numbers can be stated using the scientific 'e' format. So 8.98*pow(10.,39.) can be more easily expressed as 8.98e39.

    3) The / used for division between integer numbers results in an integer. So from your code 4/3*3.1415 results in the value of 3.1415. Because 4 and 3 are integers and 4/3 results in 1 which is then promoted to a float so the result is 3.1415! What you should write is 4.0/3.0*3.1415 which gives the required result 4.18667.

    Do you know what sort of figures you are expecting? These are what I got after making the above changes

    100.0,8980000000000000600000000000000000000000.0
    2500.0,8980000000000000600000000000000000000000.0
    5000.0,8980000000000000600000000000000000000000.0
    7500.0,8980000000000000600000000000000000000000.0
    10000.0,8980000000000000600000000000000000000000.0
    12500.0,8980000000000000600000000000000000000000.0
    15000.0,8980000000000000600000000000000000000000.0

  4. #34
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,996

    Re: Trying to create a vector using a structure definition as the basis in VC++

    I've had a look at what you are trying to calculate and you have a problem using standard cmath library. The type double is represented as 8 bytes of memory - 64 bits - as per IEEE 754 standard. This is divided up as

    52 bits for mantisa
    11 bits exponent
    1 bit mantisa sign

    The problem you are encountering is with the 52 bits for the mantisa. This gives a maximum of 17 significant decimal digits of precision. In your case you start with 8.98e39 and then subtract 58891567.970510364 in the for loop. Unfortunately with only 52 bits for the mantisa, this has no effect on the stored number which stays the same! So in the next loop R-uni hasn't changed so at the end of the loop R-uni has the same value as at the start of the loop! The IEEE 754 standard can't give sufficient precision for your requirements as you are subtracting a relative small number from a large number. Effectively, you need 40 significant decimal digits of precision which the standard c++ library can't provide.

    8980000000000000000000000000000000000000
    0000000000000000000000000000000058891567

    The coloured numbers are effectively the only ones stored. The other digits in the number are presumed to be always 0.

  5. #35
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Thank you. That seems consistent with my prior impression.

    I'll have to look into "long double" as an alternative.

    I suspect that there may be an error in the formula.

    I'll have to check it, or perhaps implement an alternative form.

    Thank you.

  6. #36
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,996

    Re: Trying to create a vector using a structure definition as the basis in VC++

    In c++, long double and double still take 8 bytes and there is no difference in terms of number of significent mantisa digits. Sorry, but using long double will make no difference.

  7. #37
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Quote Originally Posted by Protocol View Post
    Thank you. That seems consistent with my prior impression.

    I'll have to look into "long double" as an alternative.
    If you're going to use numbers and math as you've shown, then you need to use a specialized library (GIMP is one such library) that does arbitrary math.

    Otherwise, yes, you need to redo your formula(s). The forumulas you do in school or what you find in textbooks many times do not work correctly on a binary computing machine because of round-off error and numerical limits as to what can be represented. That's why many formulas have alternate forms just so they can be used on such machines.

    Regards,

    Paul McKenzie

  8. #38
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Quote Originally Posted by Paul McKenzie
    If you're going to use numbers and math as you've shown, then you need to use a specialized library (GIMP is one such library) that does arbitrary math.
    Small typo there as the library is: GMP
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 3 of 3 FirstFirst 123

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center