Thread: geometery prob/prog

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    geometery prob/prog

    Problem: can not get past 30 feet in square area formula and function. When 31 is entered in the "side" window and the checkbox/pushbutton is pressed even with the changes to code and recompiled
    i still get the answer of -2147483648.000000 period. i have changed the call and function from atoi to atol and wtold and the function changes from float to double and long double and still stuck at 31 feet = -2147483648.000000 period.
    even if i use unsigned still can not get passed 30 feet. tried powl() that did not fix it eighter.


    Code:
       
    /* functions */
    
    /* Square */
    /* Area */
    
    ASqr( float S )
    {
        float A;
    
        A = pow( 2, S ) ;
    
    return A;
    }
    
    ....
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    
       char Buffer[50];
    
       char tmpbuffer[50];
    
    ....
    
       			Client_Edit_Side = CreateWindowEx(WS_EX_STATICEDGE, "Edit", "side", WS_CHILD | WS_VISIBLE, 5, 200, 85, 15, hwnd, NULL, hInstance, 0);
    				SendMessage(Client_Edit_Area , WM_SETFONT, (WPARAM)hDefaultFont, MAKELPARAM(FALSE, 0));
    
    
    ....
    
    
    
    
    			Client_Edit_Checkbox1 = CreateWindowEx(WS_EX_STATICEDGE, "Button", "Square Area", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE, 240, 20, 100, 25, hwnd, (HMENU)SQUAREAREA	, hInstance, 0);
    				SendMessage(Client_Edit_Checkbox1,WM_SETFONT, (WPARAM)hDefaultFont, MAKELPARAM(FALSE, 0));
    
    
    
    
    
    
    ....
    
                                                                                                   case SQUAREAREA:
                                                                                                   {                                                                                                     
                                                                                                        SetWindowText(Client_Edit_Message, "Square Area");
    
    							                                            GetWindowText(Client_Edit_Side, Buffer, sizeof(Buffer));
    
                                                                                                        S = atoi(Buffer) ;
    
                                                                                                        A = ASqr(S);
    
                                                                                                        sprintf(tmpbuffer , "%f" , A);
    
                                                                                                        /* holdforstringcat */
    
                                                                                                        SetWindowText(Client_Edit_Message, tmpbuffer );
                                                                                                        
    	
                                                                                                   }
                                                                                                    break;
    
    
    ....
    S = atoi(Buffer) ; changed to S = wtold(Buffer) ; got undefined function then changed to S = atol(Buffer) ; also tried s = atof(buffer);



    Code:
    ASqr( double S )
    {
        long double A;
    
        A = pow( 2, S ) ;
    
    return A;
    }
    changed to unsigned long and still stuck. not sure if prob is in the atof call or the sprintf( ... "%f" even tried "%lu" or with the
    buffer read. or sendmessage call.

    prog. enter data in window press checkbox of geometry then call formula function then return answer then display answer in other answer window.

    second question.... why is there no BS_unsetcheck in the checkbox?
    Last edited by kryptkat; 01-07-2007 at 12:28 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    shouldn't be
    Code:
    double ASqr( double S )
    {
        long double A;
    
        A = pow( 2, S ) ;
    
    return A;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    changed to that

    enter 31 get 2147483648.000000 but when enter more than (31 )that i get 0 .... still not working

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Seems like a lack of prototypes, given your first attempt - did you include relevant header files.

    Maybe it's an operator overload problem, and using 2 means you get the integer version.
    Try say pow(2.0,S)

    Also, how long is your wndProc() function to get indentation all the way off the side of the page?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think you should check consistensy of all types you use in your calculations
    following code works for me without problems
    Code:
    #include <iostream>
    #include <cmath>
    long double ASqr( double S )
    {
        long double A;
    
        A = pow( 2, S ) ;
    
    return A;
    }
    int main()
    {
    	long double A =ASqr(32);
    	std::cout<< A;
    	return 0;
    }
    check that you have no warnings about undefined functions and significant bit looses
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Try sprintf with %g. Then it should work.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    >> Seems like a lack of prototypes,
    mabey

    >> did you include relevant header files.
    have stdio.h stdlib.h math.h windows.h that is it.

    >>Maybe it's an operator overload problem, and using 2 means you get the integer version.
    Try say pow(2.0,S)

    forgot to change one unsigned but when changed it did work somewhat. did change pow to powl() function.

    Code:
    /* functions */
    
    /* Square */
    /* Area */
    
    double ASqr( double S )
    {
        double   A;
    
        A = powl( 2, S ) ;
    
    return A;
    }
    this is what it looks like now.

    >>wndProc() function

    good sized prog. about 400k small huh?

    if i enter over 300 it locks up now

    also thank you all for the replys.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Try this. This is an exact replica of your code, but somehow it works.
    Code:
    #include <windows.h>
    #include <math.h>
    #include <stdio.h>
    #define SQUAREAREA 1000
    
    double ASqr( float S ){
        long double A;
        A=pow(2,S);
        return A;
    }
    HWND Client_Edit_Side,Client_Edit_Checkbox1,Client_Edit_Message;
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    HINSTANCE hInst;
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil){
        HWND hwnd;
        MSG messages;
        WNDCLASSEX wincl;
        hInst=hThisInstance;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&wincl)){
            return 0;
        }
    
        hwnd=CreateWindowEx(0, szClassName, "Windows App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
            544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL);
    
        ShowWindow (hwnd, nFunsterStil);
    
        while (GetMessage(&messages, NULL, 0, 0)){
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
    
        return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
        char Buffer[50];
        char tmpbuffer[50];
        switch (message){
            case WM_CREATE:{
                HFONT hDefaultFont=(HFONT)GetStockObject(DEFAULT_GUI_FONT);
                Client_Edit_Message=CreateWindowEx(WS_EX_STATICEDGE,"Edit","side",WS_CHILD|WS_VISIBLE,100,200,85,
                    15,hwnd,NULL,hInst,0);
                SendMessage(Client_Edit_Message,WM_SETFONT,(WPARAM)hDefaultFont,MAKELPARAM(FALSE,0));
                Client_Edit_Side=CreateWindowEx(WS_EX_STATICEDGE,"Edit","side",WS_CHILD|WS_VISIBLE,5,200,85,
                    15,hwnd,NULL,hInst,0);
                SendMessage(Client_Edit_Side,WM_SETFONT,(WPARAM)hDefaultFont,MAKELPARAM(FALSE,0));
                Client_Edit_Checkbox1=CreateWindowEx(WS_EX_STATICEDGE,"Button","Square Area",BS_AUTOCHECKBOX|
                    WS_CHILD|WS_VISIBLE,240,20,100,25,hwnd,(HMENU)SQUAREAREA,hInst,0);
                SendMessage(Client_Edit_Checkbox1,WM_SETFONT,(WPARAM)hDefaultFont,MAKELPARAM(FALSE, 0));
                break;
            }
            case WM_COMMAND:
                switch(wParam){
                    case SQUAREAREA:{
                        double S,A;
                        SetWindowText(Client_Edit_Message,"Square Area");
                        GetWindowText(Client_Edit_Side, Buffer, sizeof(Buffer));
                        S = atoi(Buffer) ;
                        A = ASqr(S);
                        sprintf(tmpbuffer,"%g",A);
                        /* holdforstringcat */
                        SetWindowText(Client_Edit_Message, tmpbuffer );
                        break;
                    }
                }
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Strange design... a part of a larger project?
    Last edited by maxorator; 01-07-2007 at 01:08 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    >> Try sprintf with %g. Then it should work.

    i get scientific notation and it locks up over 200 entered in window (the prog i made )
    thank you for the tip.
    Last edited by kryptkat; 01-07-2007 at 01:09 PM.

  10. #10
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    max your demo code works. when i enter 4000 i get pow overflow error.

    ok chaned mine from atof to atoi(again) it works now


    thank you all.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I've probably missed something obvious, but if you're trying to calculate the area of a square don't you want S^2 rather than 2^S?

  12. #12
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    yes S^2 is what i should have done. functions was from another function doing something diff. i thought exp went first untill i tested it.

    Code:
    /* powtest.c */
    
    
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    
       int a;
       int b;
       int d;
    
    
       a = 3;
       b = 9;
    
       d = pow( 3 , 9 ) ;
    
    printf("\n\n pow ( 3 , 9 ) is %d " , d);
    
       d = pow ( 9 , 3 ) ;
    
    printf("\n\n pow ( 9 , 3 ) is %d " , d);
    
    return 0;
    
    }

    C:\borland\bcc55\bin>powtest


    pow ( 3 , 9 ) is 19683

    pow ( 9 , 3 ) is 729
    C:\borland\bcc55\bin>
    also changed to 2.0 for ^.

Popular pages Recent additions subscribe to a feed