-2

How to convert rpm to rad/s in C? I am using Altera Monitor program to compute rad/s from rpm. but when i use 2*pi/60 it says "pi" is not declared.I have included math.h still it same issue.

also i have another problem when including vhdl floating point library. I am using Quatus 16.1 version. I tried to include

LIBRARY ieee; use ieee.float_pkg.all;

Error (10481): VHDL Use Clause error at uart.vhd(27): design library "ieee_proposed" does not contain primary unit "float_pkg". Verify that the primary unit exists in the library and has been successfully compiled.

how to solve this?

gobsa89
  • 15
  • 2

2 Answers2

3

When using gcc (and many other compilers) the constant for pi is defined in math.h as M_PI:

rads = rpm * 2.0 * M_PI / 60.0;

However this is not guaranteed by the C standard, so if your code needs to be portable then you should have a contingency definition for pi.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • This is indeed not correct always, https://stackoverflow.com/questions/5007925/using-m-pi-with-c89-standard - while available in GlibC for example, *"These constants come from the Unix98 standard and were also available in 4.4BSD; therefore they are only defined if '_BSD_SOURCE' or '_XOPEN_SOURCE=500', or a more general feature select macro, is defined. The default set of features includes these constants."* I guess Altera uses GCC however. – Antti Haapala -- Слава Україні Jun 25 '17 at 07:37
  • @AnttiHaapala: you may be right, but I've never encountered a situation where M_PI is not defined, for C at least (C++ is another matter). – Paul R Jun 25 '17 at 07:40
  • `gcc pi.c -lm -pedantic -std=c11` for example – Antti Haapala -- Слава Україні Jun 25 '17 at 07:43
  • 1
    @PaulR : For Microsoft compilers the non-standard constants including `M_PI` are guarded by `_USE_MATH_DEFINES` which must be defined either globally or before including math.h (https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx). – Clifford Jun 25 '17 at 08:12
  • 2
    @AnttiHaapala : The Altera NIOSII compiler is GCC with Newlib. In Newlib's math.h, `M_PI` is guarded only by `__STRICT_ANSI__` (hence it is undefined when `-pedantic -std=c11` is used for example). For portability it is perhaps easier to define your own constants. – Clifford Jun 25 '17 at 08:31
  • Since 2 π / 60 ≈ 0.1047197551196597746154214461093167628066, you can also say *radians per second* ≈ 0.1047197551196597746154214461093167628066 × *rotations per minute*. The constant `0.1047197551196597746154214461093167628066` is correct to at least 128 bits of precision (computed using Maple), and should suffice for any ordinary purposes. – Nominal Animal Jun 25 '17 at 16:13
1

The constant often defined in math.h is M_PI or sometimes PI, but not pi. But none of these constants are mentioned in the C standard, so you shouldn't rely on it and just define your own, e.g. using the name you expected:

#define pi 3.14159265358979323846