Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 12th, 2004, 10:52 AM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default Square root

Hi,

Im currently making a calculator in c++.net 2003.

I've got it to 'square' numbers and produce a result. Heres the code:-

void CAssignmentDlg::OnBnClickedButton21()
{
    m_fSquare=m_fSquare+atof(m_sDisplay);
    m_sDisplay="";
    m_fSquareRes = m_fSquare * m_fSquare;
    char strSquare[20];
    m_fmem=m_fSquareRes;
    gcvt(m_fmem,6,strSquare);
    for (int i = 0;i<20;i++)
    m_sDisplay = m_sDisplay+strSquare[i];

    UpdateData(FALSE);
}

My problem is how to make another button produce the square rrot of a number, it is essentially the inverse of this code but i really dont know how to do it!

Could someone give me a hand?
Thanks
Ash:D

My new web design domain
www.askmultimedia.co.uk
__________________
My new web design domain
www.askmultimedia.co.uk
Reply With Quote
  #2 (permalink)  
Old December 12th, 2004, 02:20 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use the sqrt() function (prototyped in math.h) to find the square root.

Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old December 12th, 2004, 03:28 PM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default

Thanks for the help, that works, would i use the same method to use other mathmatical functions?

For example sin() would be

m_fSin=m_fSin+atof(m_sDisplay);
    m_sDisplay="";
    m_fSinRes = sin(m_fSin);
    char strSin[20];
    m_fmem=m_fSinRes;
    gcvt(m_fmem,6,strSin);
    for (int i = 0;i<20;i++)
    m_sDisplay = m_sDisplay+strSin[i];

    UpdateData(FALSE);

Ive just tried that and it compiles, but gives me a strange result when testing, i type 45 then sin() and the answer should be 0.707, yet i get 0.850904???????

Is this anything to do with the fact its a floating point number? 'just a guess by the way!'

Thanks
Ash

My new web design domain
www.askmultimedia.co.uk
Reply With Quote
  #4 (permalink)  
Old December 12th, 2004, 04:21 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sin, cos, tan, etc all use radians as arguments and the sin of 45 radians is .859035247...

These are double precision functions, and give about 16 correct significant decimal digits. So floating point roundoff for these functions would not result in an error as large as you guessed.

Regards,

Dave
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
drawing a square wave nyc9 Visual C++ 1 December 31st, 2006 09:16 AM
root directories BrendonMelville Linux 2 December 14th, 2006 08:44 AM
Square Brackets in Form Names dylan_b JSP Basics 1 July 11th, 2006 02:29 PM
Getting Root Password harsh_hot Linux 1 September 11th, 2005 03:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.