C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 14th, 2012, 03:32 AM
pelikan67 pelikan67 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 pelikan67 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 32 sec
Reputation Power: 0
Unable to verify given condition in function

Hi,

I have these 2 conditions to include in the function so as to detect x < 0 and when x = 0.1.

I have managed to include condition 1 and are able to detect any values that is less than 0. But for condition 2, when x = 0.1, the programme unable to detect value at 0.1

I have tried setting x to be float or double or int, but to no avail.
Below is the condition and the programme that I have compiled so far, please advise on how to include the condition 2 into the function. Thank you.

condition 1: if ‘x’ is less than 0, display a message ”logarithms is only defined for positive real numbers”

condition 2: if ‘x’ = 0.1, display a message “function g(x,z) is not defined at x = 0.1”

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;
/*Formula Evaluation*/


void fun_1(void);

main()
{
int choice;
printf("Welcome to the function calculation!\n\n");

while(1){
printf("Please select the function: \n\n\t1.f(x,y)=5x^2+9xy-6y^3\n");
scanf("%d", &choice);

switch(choice)
{
case 1:
fun_1(); // Function to calculate f(x,y)=5x^2+9xy-6y^3
break;
}
system("PAUSE");
return EXIT_SUCCESS;}

}

void fun_1(void)
{
float f, x, y; // Function to calculate f(x,y)=5x^2+9xy-6y^3
printf("Please enter the value of x: ");
scanf("%f", &x);
if (x <= 0) {printf("\nlogarithms is only defined for positive real number!\n\n");}

else if (x ==0.1) {printf(“function g(x,z) is not defined at x =0.1”);}
else {
printf("\nPlease enter the value of y: ");
scanf("%f", &y);
f = (5)*(x*x)+(9)*(x)*(y)-(6)*(y*y*y);
printf("\nThe answer to this function is = %.3f\n\n", f);}
}

Reply With Quote
  #2  
Old September 14th, 2012, 04:03 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
You need to read What Every Programmer Should Know About Floating-Point Arithmetic.

Note that the value 0.1 is not exactly representable in binary (neither as a float or as a double). The binary representation of 0.1 as a float is different than the binary representation of 0.1 as a double. Your x variable is of type float, so the condition (x == 0.1) will never be true (*): the conversion of the float value in x to double will have a bunch of 0's at the end; the double value itself will almost certainly have a bunch of 1's in the final bits.

In the absence of a strong reason otherwise, use double for floating-point values. Your teacher saying to use float is not a strong reason until you've argued about double being better and failed to convince her.

(*) It can be true on some computers where the representation of float and double are the same.

Reply With Quote
  #3  
Old September 14th, 2012, 04:10 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 31 m 35 sec
Reputation Power: 1985
I'm sorry, but whatever kind of sense am I supposed to make of that unreadable mess you posted?

Let's try it with code tags (and getting rid of that damned fool K&R formatting):

Code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;
/*Formula Evaluation*/


void fun_1(void);

main()
{
    int choice;
    printf("Welcome to the function calculation!\n\n");

    while(1)
    {
        printf("Please select the function: \n\n\t1.f(x,y)=5x^2+9xy-6y^3\n");
        scanf("%d", &choice);

        switch(choice)
        {
        case 1: 
            fun_1(); // Function to calculate f(x,y)=5x^2+9xy-6y^3
            break;
        } 
        system("PAUSE");
        return EXIT_SUCCESS;
    }

}

void fun_1(void)
{
    float f, x, y; // Function to calculate f(x,y)=5x^2+9xy-
6y^3
    printf("Please enter the value of x: ");
    scanf("%f", &x);
    if (x <= 0) 
    {
        printf("\nlogarithms is only defined for positive real number!\n\n");
    }

    else if (x ==0.1) 
    {
        printf(“function g(x,z) is not defined at x =0.1”);
    }
    else 
    {
        printf("\nPlease enter the value of y: ");
        scanf("%f", &y);
        f = (5)*(x*x)+(9)*(x)*(y)-(6)*(y*y*y);
        printf("\nThe answer to this function is = %.3f\n\n", f);
    } 
}


Second condition? What second condition? You have a break condition out of the switch statement, but none out of the while loop. And the main function, which implicitly promises an int return value, returns nothing whatsoever.

Now that your ungodly mess has been formatted to make some kind of sense, just what the hell are you talking about?

Reply With Quote
  #4  
Old September 14th, 2012, 04:33 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by dwise1_aol
What second condition?

The second condition in the fun_1() function, perhaps?

Reply With Quote
  #5  
Old September 14th, 2012, 04:38 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 31 m 35 sec
Reputation Power: 1985
It's an unreadable mess. Whatever sense are we supposed to make of any of it? It is the responsibility of the individual posting the problem to present it in a manner that we can make some kind of sense of it. That individual has failed to do so.

Reply With Quote
  #6  
Old September 14th, 2012, 07:52 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
x == 0.1 is unlikely to resolve to true while x is a binary-floating-point type. 0.1 cannot be precisely represented.

Instead you need to define some small value of acceptable error and compare against that thus:

Code:
static const epsilon = 0.000001 ;

...

if( std::fabs(x - 0.1) <= epsilon )
{
    ....
}


BTW if <cstdlib>, why not <cstdio> and <cmath>. <cmath> adds overloads for math functions for types other than just double. Also why <iostream> and <stdio.h>!? Your code does not in fact use <iostream> but would probably benefit from doing so in preference to <stdio.h>. In fact as it stands it does not use <math.h> either, though <cmath> would be required for std::fabs().

Reply With Quote
  #7  
Old September 14th, 2012, 09:33 AM
pelikan67 pelikan67 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 pelikan67 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 32 sec
Reputation Power: 0
Apologies

Apologies that my post is in a mess. I have just begin to learn c++ for about a month and trying to code a function f(x,y) = 5x^2 + 9xy - 6y^3 and to verify the correctness of the given functions for various input of x:

→ if ‘x’ is less than 0, display a message ”logarithms is only defined
for positive real numbers”

→ if ‘x’ = 0.1, display a message “function g(x,z) is not defined at x =
0.1”

thank you.

Reply With Quote
  #8  
Old September 14th, 2012, 03:17 PM
mike65535's Avatar
mike65535 mike65535 is offline
um, Hello?
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2004
Location: FN23fc
Posts: 719 mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level)mike65535 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 42 m 56 sec
Reputation Power: 159
Quote:
function g(x,z) is not defined at x = 0.1
Is there really a function g(x,z) ?

What is it or why is it invalid at x = 0.1?

I'm not sure what your overall goal is... are you to provide a way for a user to enter a value for x and y (and z?), and then generate f(x,y) ?

How, then, do you intend to "verify the correctness"? Perhaps, hand calculate the answers and compare to your program?
__________________
"America's abundance was created not by public sacrifices to "the common good," but by the productive genius
of free men who pursued their own personal interests and the making of their own private fortunes. They did not
starve the people to pay for America's industrialization. They gave the people better jobs, higher wages and
cheaper goods with every new machine they invented, with every scientific discovery or technological advance --
and thus the whole country was moving forward and profiting, not suffering, every step of the way."
--Ayn Rand

Reply With Quote
  #9  
Old September 15th, 2012, 12:11 AM
pelikan67 pelikan67 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 pelikan67 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 32 sec
Reputation Power: 0
Thanks for advise

Hi mike65535,

thank you for the prompt reply. Yes, there is another function g(x,z) which is the second function that I am supposed to code, unfortunately I am already stuck in coding the first function.

Reply With Quote
  #10  
Old September 15th, 2012, 02:53 AM
pelikan67 pelikan67 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 pelikan67 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 32 sec
Reputation Power: 0
A Big Thank you to all

Hi everyone,

A big thank you for your advises. Basing on the advises from you guys, I have managed to include both conditions into the code.

Thousand of apologies for causing the confusion.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Unable to verify given condition in function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap