math.h error => C language


 
Thread Tools Search this Thread
Top Forums Programming math.h error => C language
# 1  
Old 02-15-2011
math.h error => C language

I don't know how to implement sqrt() function in C languge.
Every time I call a compiler I get this error

Code:
function sqrt not declared

I looked at C standard libray reference and from my perspective everthing is O.K

Here is the code

// Program asks user to enter the sides of the triangle
// and then test if triangle is isoscelesm , right-angled, equilateral
// Solution for exercise 17 (chapter 5)
// Date of creation: 15/02/2011

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

int main(void) {
    
    float a,b,c;
    
    // Asks user to enter sides
    
    printf("\t\tTriangle detection triangle\n");
    printf("Enter three sides of the triangle: ");
    scanf("%f, %f, %f", &a, &b, &c);
    
    if (a <= 0 || b <= 0 || c <= 0) {
        printf("Invalid input\n");
        return 1;
        }
    else if (a + b > c && a + c > b && b + c > a) {
        printf("Valid entry!\n");
        }
    if ( a != b && a != c && b != a && b != c && c != a) {
        printf("isoscelesm\n");
        }
    else if ( a == b && a == c ) { 
        printf("equilateral\n");
        }
    else if ( b == a && b == c ) {
        printf("equilateral\n");
        }
    else if ( c == a && c == b) {
        printf("equilateral\n");
        }
    else if (c == sqrt (a * a  + b * b)) {
        printf("equilateral\n");
        }
    
    return 0;
}

# 2  
Old 02-15-2011
Code:
cc -o myprog  myprog.c -lm

Try this and report back what you get.
# 3  
Old 02-16-2011
It compiled under Linux, under Solaris doesn't pass compilation.

Thnx
# 4  
Old 02-16-2011
In what manner did it not "pass compilation"? I left my crystal ball at home today.

You should also realize that "==" isn't really meant for floating point numbers. It will only return true when the values are exactly, exactly the same to a ludicrous degree of precision you'll likely never find in real life. For instance, this code fails on my system:
Code:
        if((10000000000000001.0-10000000000000000.0) == 1.0)
        {
                printf("equal\n");
        }

The greater or less-than operators are safe though, so if(abs(b-c) < 0.000001) will be better behaved than if(b==c). Pick a small enough value of 0.000...1 to match the level of precision you want.

Last edited by Corona688; 02-16-2011 at 11:40 AM..
# 5  
Old 02-16-2011
What are the exact errors? What's the exact command you're using to try and compile?
# 6  
Old 02-17-2011
I wrong typed command Smilie

Everything works, plase lock the theme
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Gcc g++ on cygwin - configure: error: *** A compiler with support for C++11 language features is req

Hi, Apologies if I posted it in a wrong section as it is related to gcc on cygwin. Please move it to appropriate section. I'm trying to compile and build libsigc++-2.10.2 on cygwin with gcc 8.3.0. when I run ./configure, I get this: I couldn't fully understand what does... (13 Replies)
Discussion started by: prvnrk
13 Replies

2. UNIX for Dummies Questions & Answers

Math

i have file (my_file.txt) that looks like this: 000000000000010000 000000000000010000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 all said and one, it should look... (11 Replies)
Discussion started by: lawsongeek
11 Replies

3. UNIX for Dummies Questions & Answers

Linux Math Help

I am struggling with scripting this challenge a friend and I have. You have file1 and its contents is a single number you have file 2 and its contents are a different number you want to add file1 to file2 and have the output be put into file3 (3 Replies)
Discussion started by: minkyboodle
3 Replies

4. Shell Programming and Scripting

Need help with AWK math

I am trying to do some math, so that I can compare the average of six numbers to a variable. Here is what it looks like (note that when I divide really big numbers, it isn't a real number): $ tail -n 6 named.stats | awk -F\, '{print$1}' 1141804 1140566 1139429 1134210 1084682 895045... (3 Replies)
Discussion started by: brianjb
3 Replies

5. Shell Programming and Scripting

math help

$ x=1 $ y=1.5 $ z=$((x*y)) bash: 1.5: syntax error: invalid arithmetic operator (error token is ".5") What's wrong? (2 Replies)
Discussion started by: rockbike
2 Replies

6. UNIX for Dummies Questions & Answers

math in unix

I have 2 variables a=2 b=1 i want to add a and b how do i do this in unix using just the echo command and by assigning it to a different variable like c? (13 Replies)
Discussion started by: khestoi
13 Replies

7. Programming

math.h in makefile

Hey all, How do I link the math library in a gnu make makefile? I have tried using -lm with the CFLAGS varibale - flags like -Wall and -ggdb work, but -lm does not. I am running gcc - 4.1.2 on a linux machine. (2 Replies)
Discussion started by: kermit
2 Replies

8. Programming

something about <math.h>

Hi, I got an easy problem for you but really difficult for me 'cause I am pretty new to this field I got header file <math.h> included in my .c file , then I write the code as below: k = sqrt(i); /* both variables k and i are int */ then I cc temp.c it says like this undefined... (4 Replies)
Discussion started by: blf0
4 Replies

9. Shell Programming and Scripting

Math calculation help

Hi, I wrote this script awk -F"\t" '{if ((($1 == 586) || ($1 == 68030)) && (($2/1024) < 512)) print $0"\t"(512-($2/1024))"\t"(512-($2/1024))/256}' pcs.txt But I want from the calculation in red to get rid of the decimal part. Like instead of 1.75 to keep only 1.Please somebody tell me what... (4 Replies)
Discussion started by: sickboy
4 Replies
Login or Register to Ask a Question