The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Unable to verify given condition in function
Discuss Unable to verify given condition in function in the C Programming forum on Dev Shed. Unable to verify given condition in function C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 14th, 2012, 03:32 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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);}
}
|

September 14th, 2012, 04:03 AM
|
|
|
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.
|

September 14th, 2012, 04:10 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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?
|

September 14th, 2012, 04:33 AM
|
|
|
Quote: | Originally Posted by dwise1_aol What second condition? |
The second condition in the fun_1() function, perhaps? 
|

September 14th, 2012, 04:38 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

September 14th, 2012, 07:52 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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().
|

September 14th, 2012, 09:33 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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.
|

September 14th, 2012, 03:17 PM
|
 |
um, Hello?
|
|
Join Date: Nov 2004
Location: FN23fc
|
|
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
|

September 15th, 2012, 12:11 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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. 
|

September 15th, 2012, 02:53 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|