473,908 Members | 4,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ anamaly

If you run this program, it gives very unexpected results. Can anyone
explain the nature of this anamaly? (also what is the function call
[and library to include linux/windows] to execute 'pause');

#include<iostre am>
using namespace std;
//void pause(){ unsigned long i=0; while(i++<10000 000); }

int main(){
int counter=0;
for(float i=1;i<100;i+=0. 1) // pay attention
{ // pause();
std::cout<<i<<" \n";
}

cout<<endl;
return 0;
}

Jul 23 '05 #1
23 1373
puzzlecracker wrote:
If you run this program, it gives very unexpected results. Can anyone
explain the nature of this anamaly? (also what is the function call
[and library to include linux/windows] to execute 'pause');

#include<iostre am>
using namespace std;
//void pause(){ unsigned long i=0; while(i++<10000 000); }

int main(){
int counter=0;
for(float i=1;i<100;i+=0. 1) // pay attention
{ // pause();
std::cout<<i<<" \n";
}

cout<<endl;
return 0;
}

I guess the float gets printed as a double (there is no operator<<
overload for float). Use double instead.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2

Ioannis Vranos wrote:
puzzlecracker wrote:
If you run this program, it gives very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call
[and library to include linux/windows] to execute 'pause');

#include<iostre am>
using namespace std;
//void pause(){ unsigned long i=0; while(i++<10000 000); }

int main(){
int counter=0;
for(float i=1;i<100;i+=0. 1) // pay attention
{ // pause();
std::cout<<i<<" \n";
}

cout<<endl;
return 0;
}

I guess the float gets printed as a double (there is no operator<<
overload for float). Use double instead.


--
Ioannis Vranos

http://www23.brinkster.com/noicys


SAME THING - I think it has to do with the way floating point numbers
are represented.

Jul 23 '05 #3
"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
If you run this program, it gives very unexpected results. Can anyone
explain the nature of this anamaly? (also what is the function call
[and library to include linux/windows] to execute 'pause');


It works just fine for me on gcc 3.3.3.

Can you describe the anomaly that happens on your system?

James
Jul 23 '05 #4
what is the last number being printed in your case?

James Aguilar wrote:
"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
If you run this program, it gives very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call
[and library to include linux/windows] to execute 'pause');


It works just fine for me on gcc 3.3.3.

Can you describe the anomaly that happens on your system?

James


Jul 23 '05 #5
puzzlecracker wrote:
If you run this program, it gives very unexpected results.
What do you think is unexpected about it ?

Can anyone explain the nature of this anamaly?
Round off error, more than likely.

0.1 is a number that can't be represented exactly.

In binary to 23 digits it is represented as the following approximation:

0.0001100110011 0011001100

Which is 1 10 millionth (approx) in error or more accurately.
1/10485760

When you multiply that error by 1000 you get an error of:
25/262144

i.e. 1 in 100,000 (approx).
(also what is the function call [and library to include linux/windows] to execute 'pause');


What is pause ? Why do you need it ?
Jul 23 '05 #6

Gianni Mariani wrote:
puzzlecracker wrote:
If you run this program, it gives very unexpected results.
What do you think is unexpected about it ?

Can anyone
explain the nature of this anamaly?


Round off error, more than likely.

0.1 is a number that can't be represented exactly.

In binary to 23 digits it is represented as the following

approximation:
0.0001100110011 0011001100

Which is 1 10 millionth (approx) in error or more accurately.
1/10485760

When you multiply that error by 1000 you get an error of:
25/262144

i.e. 1 in 100,000 (approx).
(also what is the function call
[and library to include linux/windows] to execute 'pause');


What is pause ? Why do you need it ?


how to correct this problem?

Jul 23 '05 #7
puzzlecracker wrote:
Gianni Mariani wrote: ....

Round off error, more than likely.

....
how to correct this problem?
Change the order of operations to minimize the round-off error
accumulation and/or use higher precision (double) numbers.

You can use exact numbers as well - like a quotient type.

This is one example I whipped up to come up with that bit pattern...
template <typename w_T>
w_T gcd( w_T i_v1, w_T i_v2 )
{
w_T l_vl;
w_T l_vs;
if ( i_v1 > i_v2 )
{
l_vl = i_v1;
l_vs = i_v2;
}
else
{
l_vl = i_v2;
l_vs = i_v1;
}

w_T l_t;
w_T l_t2;

do {

l_t2 = l_vl % l_vs;

l_t = l_vl;
l_vl = l_vs;
l_vs = l_t2;
} while( l_t2 );

return l_vl;
}
struct quotient
{
typedef unsigned long long t_type;

bool m_sign;
t_type m_numerator;
t_type m_denominator;

quotient(
bool i_sign,
t_type i_numerator,
t_type i_denominator
)
: m_sign( i_sign ),
m_numerator( i_numerator ),
m_denominator( i_denominator )
{
Simplify();
}

void Simplify()
{
t_type l_gcd = gcd( m_numerator, m_denominator );

if ( l_gcd != 1 )
{
m_numerator /= l_gcd;
m_denominator /= l_gcd;
}
}

quotient operator-() const
{
return quotient( !m_sign, m_numerator, m_denominator );
}

};

quotient operator+( const quotient & i_lhs, const quotient & i_rhs )
{
quotient::t_typ e l_demgcd = gcd( i_lhs.m_denomin ator,
i_rhs.m_denomin ator );

quotient::t_typ e l_mul_rhs = i_lhs.m_denomin ator / l_demgcd;
quotient::t_typ e l_mul_lhs = i_rhs.m_denomin ator / l_demgcd;
quotient::t_typ e l_den = l_mul_lhs * i_lhs.m_denomin ator;

if ( i_lhs.m_sign == i_rhs.m_sign )
{
return quotient(
i_lhs.m_sign,
i_lhs.m_numerat or * l_mul_lhs + i_rhs.m_numerat or * l_mul_rhs,
l_den
);
}
else
{
quotient::t_typ e l_num_lhs = i_lhs.m_numerat or * l_mul_lhs;
quotient::t_typ e l_num_rhs = i_rhs.m_numerat or * l_mul_rhs;

if ( l_num_lhs > l_num_rhs )
{
return quotient(
i_lhs.m_sign,
l_num_lhs - l_num_rhs,
l_den
);
}
else
{
return quotient(
i_rhs.m_sign,
l_num_rhs - l_num_lhs,
l_den
);
}
}

}

quotient operator-( const quotient & i_lhs, const quotient & i_rhs )
{
return i_lhs + - i_rhs;
}

quotient operator*( const quotient & i_lhs, const quotient & i_rhs )
{
return quotient(
i_rhs.m_sign != i_rhs.m_sign,
i_lhs.m_numerat or * i_rhs.m_numerat or,
i_lhs.m_denomin ator * i_rhs.m_denomin ator
);
}

quotient operator/( const quotient & i_lhs, const quotient & i_rhs )
{
return quotient(
i_rhs.m_sign != i_rhs.m_sign,
i_lhs.m_numerat or * i_rhs.m_denomin ator,
i_lhs.m_denomin ator * i_rhs.m_numerat or
);
}

template<
typename i_char_type,
class i_traits

basic_ostream<i _char_type, i_traits>& operator << (
basic_ostream<i _char_type, i_traits> & i_ostream,
const quotient & i_value
) {

if ( i_value.m_sign )
{
i_ostream << '-';
}

i_ostream << i_value.m_numer ator;

if ( i_value.m_denom inator != 1 )
{
i_ostream << '/' << i_value.m_denom inator;
}

return i_ostream;
}

//////////////////// example of you to use quotient this //////////////

int main()
{
quotient m_value( false, 1, 10 ); // 1/10th or 0.1

quotient m_bit( false, 1, 2 ); // 1/2 or 0,5 - the first bit.

for ( int i = 23; i --; )
{
quotient m_diff = m_value - m_bit;

if ( m_diff.m_sign )
{
std::cout << '0';
}
else
{
std::cout << '1';
m_value = m_diff;
}

m_bit.m_denomin ator <<= 1;
}

std::cout << " " << m_value << " " << quotient( false, 1000, 1 ) *
m_value;
}
Jul 23 '05 #8
On 29 Jan 2005 22:58:24 -0800 in comp.lang.c++, "puzzlecrac ker" <ir*********@gm ail.com> wrote,
how to correct this problem?


Don't use floating point types for loop counters.
Use long.

Jul 23 '05 #9
"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
what is the last number being printed in your case?


99.999 -- the correct number, or, at least, the behavior I would expect.

As I'm sure you know, there is calculation error in all floating point
numbers -- both float, double, and long double. The extent of the error is
dependent upon the hardware, but I'm sure that your teachers have told you
the danger of comparing fps as equal. How is it different if you do 10000
ops on one?
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1670
by: puzzlecracker | last post by:
If you run this program, it will give very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call to execute 'pause'); #include<stdio.h> //void pause(){ unsigned long i=0; while(i++<10000000); } int main(){ float i;
0
11335
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10536
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3354
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.