C++ Loops
#1
Scooby Regular
Thread Starter
Join Date: Nov 2002
Location: Bonnie Scotland
Posts: 784
Likes: 0
Received 0 Likes
on
0 Posts
I am looking for help in creating a program which takes a number and continuisly divides the answer by 2 until the difference is less than 0.00005.
Can any Computer experts help me ( pretty computer illiterate )?
Cheers
Big Daz
Can any Computer experts help me ( pretty computer illiterate )?
Cheers
Big Daz
#6
Scooby Regular
Thread Starter
Join Date: Nov 2002
Location: Bonnie Scotland
Posts: 784
Likes: 0
Received 0 Likes
on
0 Posts
Homework, Who mentioned Homework !!
Can paint pretty pictures and calculate train fares, just can't get my head round this. Maybe better just carry on with what I get paid for.
Can paint pretty pictures and calculate train fares, just can't get my head round this. Maybe better just carry on with what I get paid for.
Trending Topics
#8
Scooby Regular
It'll compile under C++ : remember, C is just a subset of C++.
If you wanted to be clever :-
class DefineDivisionClass
{
// Class Variable
float m_Value;
// Constructor
void DefineDivisionClass( float value )
{
m_Value = value;
while( m_Value > 0.0005 )
{ m_Value = m_Value / 0.5f; }
}
// Destructor
~DefineDivisionClass ()
// Access Value variable
float GetValue( void )
{ return m_Value; }
{};
// Member functions
};
{
.
.
.
DefineDivisionClass TheClass( 2.0 );
float result = TheClass.GetResult();
.
.
.
}
If you wanted to be clever :-
class DefineDivisionClass
{
// Class Variable
float m_Value;
// Constructor
void DefineDivisionClass( float value )
{
m_Value = value;
while( m_Value > 0.0005 )
{ m_Value = m_Value / 0.5f; }
}
// Destructor
~DefineDivisionClass ()
// Access Value variable
float GetValue( void )
{ return m_Value; }
{};
// Member functions
};
{
.
.
.
DefineDivisionClass TheClass( 2.0 );
float result = TheClass.GetResult();
.
.
.
}
#12
Scooby Regular
Join Date: Nov 1999
Location: Stockport
Posts: 474
Likes: 0
Received 0 Likes
on
0 Posts
spot the bug time
// Constructor
void DefineDivisionClass( float value )
{
m_Value = value;
while( m_Value > 0.0005 )
{ m_Value = m_Value * 0.5f; } // should be * 0.5 rather than / 0.5
}
// Constructor
void DefineDivisionClass( float value )
{
m_Value = value;
while( m_Value > 0.0005 )
{ m_Value = m_Value * 0.5f; } // should be * 0.5 rather than / 0.5
}
#13
Re: "It'll compile under C++ : remember, C is just a subset of C++."
Hum.
C arrived first, by many years.
C++ is mostly a superset of C, mostly.
Moray
(thinks: " for ( ; value > 0.00005 ; value/=2.0F ){}; " )
Hum.
C arrived first, by many years.
C++ is mostly a superset of C, mostly.
Moray
(thinks: " for ( ; value > 0.00005 ; value/=2.0F ){}; " )
#14
Since we are bug fixing...
As long as the number is a whole number between 1 and 214747 mine's the quickest.
If the number has to be a decimal or out of those bounds change the parameter to a double and it's still quickest.
:P
[EDIT] Actually it dawns on me that I am solving a different problem to everyone else PMSL.
Before I go back to sleep is the final value important or did you just want the count?
[Edited by MrDeference - 3/26/2003 5:28:22 PM]
As long as the number is a whole number between 1 and 214747 mine's the quickest.
If the number has to be a decimal or out of those bounds change the parameter to a double and it's still quickest.
:P
[EDIT] Actually it dawns on me that I am solving a different problem to everyone else PMSL.
Before I go back to sleep is the final value important or did you just want the count?
[Edited by MrDeference - 3/26/2003 5:28:22 PM]
#16
Well, yeah.
You know I have been sat here looking at it, and it's only algebra. Something that could be solved with a one hit equation. No iteration, nothing.
Can I work out what it is? Nope.
You know I have been sat here looking at it, and it's only algebra. Something that could be solved with a one hit equation. No iteration, nothing.
Can I work out what it is? Nope.
Thread
Thread Starter
Forum
Replies
Last Post
Mister:E
Subaru Parts
2
24 September 2015 01:37 PM