Jump to content

sqrt function in c in a linux environment with gcc as the compiler

Souranil21 chakraborty
hey anyone help me with a function in c? i was trying to use the sqrt function to determine the squareroot of an positive interger number through a c program so i included the <math.h> header file and also i am on a linux environment with gcc compiler. So is there any other way to determine the squareroot of a number in a linux environment

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Souranil21 chakraborty said:

hey anyone help me with a function in c?

What are you getting instead of the right answer? Error message?

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | a 10G NIC (pending) | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4x 10TB WD Whites / 4x 14TB Seagate Exos / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X540-T2 10G NIC | LSI 9207-8i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

there are dozens of alternative math libraries for C but... what's wrong with math.h? Did you forget to link it using the

-lm

gcc parameter?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Souranil21 chakraborty said:
hey anyone help me with a function in c? i was trying to use the sqrt function to determine the squareroot of an positive interger number through a c program so i included the <math.h> header file and also i am on a linux environment with gcc compiler. So is there any other way to determine the squareroot of a number in a linux environment

 

The answer can really depend on what you are trying to do, and why you are wanting to use something other than the sqrt function.

 

Could be wrong, but from what I though sqrt in effect gets hardware accelerated (like using the SSE/SSE2 assembly commands for sqrt)...so the processor handles square roots itself (which should make it a lot quicker than code written by hand).

 

I mean, if lets say sqrt isn't doing things the way you want it to (lets say you are trying to processes a weird data type), then you could always just write the sqrt function yourself.  [There are decent examples of C code sqrt functions, even a "fast" but less accurate sqrt function].  I mean Newton's method is fairly simple to implement and decently fast.

 

https://scriptverse.academy/tutorials/c-program-newton-raphson.html

^- Newton's method in C.  No libraries needed at all [Note they hardcoded it to find the value of sqrt(5), but that is easily fixed]

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Sauron said:

there are dozens of alternative math libraries for C but... what's wrong with math.h? Did you forget to link it using the

-lm

gcc parameter?

no i  linked it through #include but when i ran the programm after compilation the sytem is throwing an error saying that the sqrt function is not recognized

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, AbydosOne said:

What are you getting instead of the right answer? Error message?

yeah the sqrt function is not recorgnized or something like that the rest of the programme is fine

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Souranil21 chakraborty said:

yeah the sqrt function is not recorgnized or something like that the rest of the programme is fine

 

see here i made a quick compilation od just simple c code and see what is the error coming

Screenshot from 2022-02-11 09-26-09.png

Screenshot from 2022-02-11 09-25-57.png

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Souranil21 chakraborty said:

see here i made a quick compilation od just simple c code and see what is the error coming

Screenshot from 2022-02-11 09-26-09.png

Screenshot from 2022-02-11 09-25-57.png

Shouldn't it be "Math.sqrt()" and not just "sqrt()"? (I might be thinking of C++ though.)

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | a 10G NIC (pending) | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4x 10TB WD Whites / 4x 14TB Seagate Exos / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X540-T2 10G NIC | LSI 9207-8i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Souranil21 chakraborty said:

see here i made a quick compilation od just simple c code and see what is the error coming

Well you should follow what @Sauron said.  You missed the -lm flag in gcc which means you aren't importing the math library.

 

A note as well, when you have issues such as that it's also really handy just googling the error code.

e.g. The top search result for "undefined reference to sqrt c" returns me to stackoverflow saying to add -lm to the gcc flag. (gcc file1.c -lm)

 

Also, you should be casting the sqrt to an int (sqrt returns a double value)

 

e.g.

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

int main() {
	int a=4, result;
	result = (int) sqrt(a);
	printf("%d", result);
	return 0;
}

Try something like that, likely will

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Souranil21 chakraborty said:

no i  linked it through #include but when i ran the programm after compilation the sytem is throwing an error saying that the sqrt function is not recognized

 

You need to link it at compilation time too or it won't be linked

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Souranil21 chakraborty said:

see here i made a quick compilation od just simple c code and see what is the error coming

 

Screenshot from 2022-02-11 09-25-57.png

You need to link the project against libm at compile time.

 

gcc file1.c -lm -o app_name

 

I would personally recommend learning to use a build system like CMake or Meson, so you can define your project properties once rather than manually at compile time.

Spoiler

image.thumb.png.969a720aba5a1f6542bdf14f45f17820.png

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×