Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,389 people online right now. Registration is fast and FREE... Join Now!




why not break;

 
Reply to this topicStart new topic

why not break;

kornni
7 Jul, 2008 - 09:16 AM
Post #1

New D.I.C Head
*

Joined: 1 Jul, 2008
Posts: 9


My Contributions
why this program do not break; when u==0

CODE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define pi   4 * atan(1.0)

float t;
double u;
int main()
{
    u = 100;
    for(t = 0; t <0.2; t += 0.01)
    {    
          u = u*sin(0.5*pi) - u*sin(0.6*pi) + u*sin(0.7*pi);
          printf("%f\n",u);
          
          if(u==0)
          {
              break;
          }      
    }
}


thanks smile.gif
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Why Not Break;
7 Jul, 2008 - 10:25 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 314



Thanked: 15 times
Dream Kudos: 225
My Contributions
While dealing with double or float it is not a good way to compare with equal because of accuracy problems, somehow I mean that .00009 might be zero for you (according to accuracy you want) but is not zero for computer
User is offlineProfile CardPM
+Quote Post

KYA
RE: Why Not Break;
7 Jul, 2008 - 10:53 AM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,045



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Works for me if u ever equals zero. Use a test value to ensure the mechanics are correct--seems fine.
User is online!Profile CardPM
+Quote Post

captainhampton
RE: Why Not Break;
7 Jul, 2008 - 11:07 AM
Post #4

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
The operations you are performing are too precise to get a rounded zero value, as opposed to the decimal values you are getting from sin, cos, etc. Perhaps try a less than condition except == 0
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Why Not Break;
8 Jul, 2008 - 10:47 PM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You are comparing a double with an integer, and conversion may result in a loss of precision.

Use this instead:
cpp
if( u == 0.0 )

This way the compiler interprets 0.0 as a double and not an integer. (if 0.0f is mentioned, the compiler will consider it as a float)
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Why Not Break;
9 Jul, 2008 - 05:57 PM
Post #6

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,461



Thanked: 10 times
Dream Kudos: 325
My Contributions
I haven't really looked at your algorithm but keep in mind you can have things like negative zero as a floating point value, which will not equal normal zero.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 10:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month