324 questions
23
votes
4
answers
2k
views
What is the difference between iszero(x) and x == 0 for floating types?
C23 added the classification macro iszero for testing whether an argument value is zero, specified like:
int iszero(real-floating x);
What is the difference between using iszero(x) and writing x == 0?...
3
votes
2
answers
234
views
C treating variable declared to be a double as an int
I am trying to solve a problem posed in this question which asks that a program be written that calculates logarithms of numbers without including <math.h>.
I wrote the following program:
#...
13
votes
4
answers
1k
views
Implicit declaration of standard function in C
This question comes from an attempt to understand the "C philosophy" and not from any "real problem".
Suppose that in a C program I use sin(x):
#include <stdio.h>
int main() ...
0
votes
2
answers
119
views
How can I find the declaration of sin function in math.h file of GCC in Debian Linux?
I read about C programming language in Yashwant Kanetkar's book Let us C. In Functions chapter I read about sin, cos, tan and pow functions of math.h file. But in this chapter it is written that:
You ...
3
votes
2
answers
141
views
In C, extract mantissa of a double as a long long in portable way
Function frexp(3) returns the normalized mantissa xm of a double x, with a value in [0.5,1.0), unless x=0. So I can convert it to a long long as follows:
int ie; // will be set in next line
...
0
votes
0
answers
44
views
Undefined reference to mathematical functions when called with variables but work fine without [duplicate]
I have a small C program:
#include <stdio.h>
#include <math.h>
int main() {
// this works
printf("%lf", sin(3.14/2));
// this doesn't work
float x = 3.14/2;
...
0
votes
1
answer
46
views
I can't pass value using variable to log from math.h
I have a problem with using log from math.h library on stm32f411ret6.
when i pass a value normally to log eg.
double var = log(2.0);
than i get 0.69314718056 in var.
When i pass a value using ...
0
votes
1
answer
186
views
<math.h> math module is not working for me in C, i am using VSCodium in linux
so my code ran smoothly in codium with the extensions when i was only working with stdio.h, but when i tried to use math.h module in my program i am unable to run it, this is what i get as error:
...
0
votes
3
answers
219
views
Arithmetic operator of power in C
I wanted to know if there is any way of doing a variable in the power of another variable (or a constant) in C without using the math.h library.
Like the operator ** in python for example.
Trying to ...
2
votes
1
answer
1k
views
Implementing a cosine function in C
I have to implement my own version of the double cos(double x) function present in the <math.h> library of C.
There are some things that I don't understand about the implementation of this ...
0
votes
0
answers
30
views
How to fix this variable undefined error in pi value when math.h is used? [duplicate]
#include<stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
void Multiply2Nos (float x, float y) {
float result = x * y;
printf("%.3f X %.3f = %.2f\n",x,y,result);
}
...
0
votes
1
answer
69
views
Calculating the geometric series with different values in C but my outputs for some values are huge
I have written code in C to calculate the geometric series for three different cases. The values for the three cases are as follows.
case 1:
n=10000,a=2.0,r=0.01.
Case 2:
n=500,a=0.01,r=1.1.
Case 3:
n=...
0
votes
0
answers
76
views
Why doesn't my code print from this if condition correctly? [duplicate]
I am writing a program in C which currently does the following:
takes 4 coordinate pairs in the form (x,y) as input
calculates the sides formed by two pairs of points, compares the corresponding ...
1
vote
0
answers
68
views
How to enter values in brackets and numbers with a degree in C console
I have an array of unsigned long long int type, the values are entered in one line through the console, among the numbers there are (2^32-1)^2-1 and (2^32-1)^2+1.
How can I convert this to a natural ...
-1
votes
2
answers
771
views
Check if a number is a perfect square in C
Task:
In an array of N elements (N < 20) of the unsigned long long type
find the maximal full square and print its index (starting from zero).
If there are no full squares, print -1.
If there is ...
2
votes
1
answer
920
views
fmin and fmax are much slower than simple conditional operator
I was working on some C++ code to process video frame, and found std::fmin and std::fmax is much slower than simply conditional operator. I've simplify my code as following (modify my code more C++ ...
-3
votes
1
answer
179
views
i keep getting infinity as an output in my code
#include <stdio.h>
#include <math.h>
int i;
double result = 0;
int terms;
int term;
double stage1;
double stage2;
double zeta(int terms){
for (i=0; i != terms; i++){
result += ...
0
votes
0
answers
66
views
C - MCU code has 3 error prone regions on my graph output
I'm trying to output the tilt angle of an accelerometer, over serial with an MCU. The code works 99% smoothly for all angles, but I keep consistently getting errors in three regions and am not sure ...
0
votes
2
answers
1k
views
Scilab 2023 Xcos electrical demo does not work in Windows 10
I've just installed Scilab 2023 in Windows 10 with Visual Studio Buildtools successfully:
--> haveacompiler()
ans =
T
--> findmsvccompiler()
ans =
"Visual Studio Build Tools 2022&...
0
votes
1
answer
312
views
How does `math.h` refer to functions like `sin` or `cos`?
I see a plenty of similar questions, which are duplicates (like 12074147), but most of them are concerned with mathematical functions implementation.
My question is more about C standard headers, like ...
1
vote
1
answer
731
views
including cmath over math.h is causing incorrect results
Edited for test case and compiler inconsistency. Recently I made a change to use the more modern < cmath > instead of < math.h >. While this compiled just fine as expected with no warnings,...
0
votes
0
answers
718
views
Trying to compile a C program with GCC and math.h header
So, I'm trying to compile a simple C program in windows environment using GCC:
#include <stdio.h>
#include <math.h>
int main(void) {
printf("hi\n");
double x = sin(10);
...
0
votes
1
answer
93
views
How do I properly link math.h library in makefiles?
I need to plot a function with gnuplot. This function requires the math.h library. Now I want to run it with a makefile.
When I run the makefile I get this output/error:
gcc -I./inc -o./build/result -...
0
votes
0
answers
541
views
problem with make file and math.h in gcc-arm-none-eabi
i'm working on arm cortex m4 with make file and gcc-arm-none-eabi on ubuntu. when i add math .h library to my project and try to use log10f , the linker exit with errors.
its be good if help my ...
1
vote
2
answers
1k
views
MPLABX math.h error . (1510) non-reentrant function "___fleq" appears in multiple call graphs and has been duplicated by the compiler
Im working on a project about active power and reactive power measurement kit with using PIC16F877A MCU. In the code, I use pow, sqrt and cos functions which are included in math.h library. when I ...
1
vote
0
answers
1k
views
Why can't Compiler Explorer find <math.h>?
I am trying to compile a C module that includes math.h on Compiler Explorer, targeted at the ARM architecture. The compiler complains that it cannot find math.h regardless of the armclang version I am ...
2
votes
1
answer
115
views
Why the erf functions have no range error?
The erfcf(FLT_MAX) produces 0.0f and sets errno to ERANGE.
The erff(FLT_MAX) produces 1.0f and does not set errno to ERANGE.
In both cases returned results differ from the "true results" (i....
0
votes
2
answers
135
views
memset with math class macros
I am trying to use memset with INFINITY or NAN present in C header file <math.h>
My code is:
double *dist;
dist = (double *)malloc(7*sizeof(double));
memset(dist, INFINITY, 7*sizeof(dist[0]));
...
0
votes
1
answer
232
views
Status of tgmath.h in terms of portability across C, C++ compilers
I'm implementing a DSL with a compiler that translates it into C. Our code is self-contained: we provide users with just one main entry point (i.e., void step()), all inputs are given in global ...
0
votes
0
answers
251
views
arm cortex log10 function sometimes returns 0
I'm using stm32f4xx mcu with <math.h> lib
when I convert the mW to dBm, used log10f() function
but, the log10 function return value is not valid occasionally.
here is my code:
volatile float ...
0
votes
0
answers
23
views
Unable to use exp() with a variable [duplicate]
This has been solved
I am trying to use exp() from math.h in C, but it is failing to compile
whenever I pass a variable as the argument. The following code works just fine
#include <math.h>
#...
2
votes
4
answers
832
views
Power function giving different answer than math.pow function in C
I was trying to write a program to calculate the value of x^n using a while loop:
#include <stdio.h>
#include <math.h>
int main()
{
float x = 3, power = 1, copyx;
int n = 22, copyn;...
-1
votes
1
answer
100
views
Why pow() function working correctly, only after saving it to a variable? [duplicate]
I were asked to c-program to accept two variables and print the answer when second variable is raised to the power of first variable. So I coded,
#include<stdio.h>
#include<math.h>
int x,...
1
vote
1
answer
3k
views
Math.h in Embedded systems
Is math.h in the standard library optimized for embedded systems especially ARM architecture (Cortex-M4)?
If not what is the fastest alternative way to use math in a microcontroller?
1
vote
2
answers
227
views
Error encountered while defining a function to calculate the Euclidean distance between two points using a struct
I created a function to calculate the Euclidean distance between two points in C (written in the Codeblocks IDE), however some errors occurred:
error: expected ')' before 'p1'|
error: expected ...
0
votes
0
answers
42
views
Trouble using trig functions c++ [duplicate]
I'm having trouble using the arcsine() function. When I use this code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << asin(12 / 13);
}
I get 0 ...
1
vote
1
answer
74
views
Unexpected output math.h [duplicate]
Does anybody knows why this code prints 1000000000000000000 and not 1000000000000000001 as expected?
#include <stdio.h>
#include <math.h>
int main(void){
long long n = (pow(10,18) + 1);...
-1
votes
1
answer
155
views
Is there a way to convert octal input to decimal/binary output without using math.h library and pow?
Beginner programmer here.
Is there a way to input octal values and expect decimal or binary outputs without the use of functions or the math.h library? I have an idea of using a loop to achieve this ...
0
votes
0
answers
44
views
What is the reason here that pow function not working correctly? [duplicate]
I am trying to write a program that gives output if the input is an integer (say 1234)
1234
234
34
4
I wrote the following code in C using codeblocks IDE :
#include <stdio.h>
#include <math....
-1
votes
1
answer
282
views
Different execution times between pow and powf of standard math.h library in c programming [closed]
I'm currently benchmarking several algorithms in C code. I recognized the following behavior I cannot explain:
When comparing the execution times of the pow() and powf() function of the math.h library,...
4
votes
2
answers
305
views
If the next representable value after -0.0 is +0.0, then why nextafter(-0.0, INFINITY) does not return +0.0?
If the next representable value after -0.0 is +0.0, then why nextafter(-0.0, INFINITY) does not return +0.0?
0
votes
3
answers
208
views
sin() output of a double coming out as 0.00000. (C language)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int i;
double interval;
for(i=0;i<30;i++)
{
interval = i/10.0;
...
0
votes
0
answers
30
views
How do I configure GCC to include header files automatically? [duplicate]
I am trying to write a code which displays the roots of a given quadratic equation and for that i need the 'math.h' header file so I can use the 'sqrt()' function, but whenever I try to run the code ...
0
votes
1
answer
50
views
analyze array in a specific direction
I have a dynamically allocated array that contains data for a piece of land:
0| 1| 2| 3| 4| 5| ->coordinates
0| 1 0 8 4 1 1
1| 7 4 2 6 7 8
2| 7 4 2 4 3 4
3| 7 1 6 ...
1
vote
1
answer
97
views
what does Format specifier int* & double* warning message mean? [closed]
My program is to get (a^b)%c .
my program -
#include <stdio.h>
#include <math.h>
double powfunction (double x, double y)
{
double answer = pow(x,y);
return answer;
}
int main()
{
...
-2
votes
2
answers
313
views
Fmod does not work as expected(c programming) [closed]
(the problem is caused by me and has been solved ,greetings from the newbie)I apologize to everyone, my function type was integer i just realized it, I opened it because I worked for hours, I have to ...
3
votes
1
answer
203
views
GCC error undefined reference to `sqrt' on changing command option order [duplicate]
Why I am getting the following error
/tmp/ccuWdVB3.o: In function `test':
MyCode.c:(.text+0x1c): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
MyCode.c
#include<stdio.h&...
0
votes
1
answer
194
views
unexpected behavior of abs(unsigned)
i just stumbled upon a really crazy bug after over a year since writing a program (it hit an assertion) regarding abs(). i'll spare you the details and i'll just show you what i don't understand here:
...
0
votes
0
answers
338
views
CLion: Can't include Math.h
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(fms C)
set(CMAKE_C_STANDARD 99)
add_executable(fms main.c fms.c fms.h fms_data.c)
target_link_libraries(fms m)
The following ...
-1
votes
1
answer
100
views
Problem compiling C progtam from command line gcc with math.h
I am new to the command line and I am trying to run a C program containing the function log10.
If I give
gcc -o -lm randomVamp random\ vampire.c
I get the error
gcc: error: randomVamp: No such file ...