<< < 5 6 7 8 9 10 11 12 13 14 15 > >>

How can I return a sequence of random numbers which dont repeat at all?
How can I return a sequence of random numbers which dont repeat at all? What you're looking for is often called a ``random permutation'' or ``shuffle.'' One way is to initialize an array with the values to be shuffled, then randomly interchange each of the cells with another one later in the array: ...
2015-07-24, 1116👍, 0💬

How can I generate floating-point random numbers?
How can I generate floating-point random numbers? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither is in the C Standard.) It's easy to write a low-pr...
2015-07-22, 1178👍, 0💬

How can I generate random numbers with a normal or Gaussian distribution?
How can I generate random numbers with a normal or Gaussian distribution? There are a number of ways of doing this. 1. Exploit the Central Limit Theorem (``law of large numbers'') and add up several uniformly-distributed random numbers: #include &lt;stdlib.h> #include &lt;math.h> #define NSU...
2015-07-22, 1122👍, 0💬

I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it?
I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither...
2015-07-20, 1092👍, 0💬

Is exit(status) truly equivalent to returning the same status from main?
Is exit(status) truly equivalent to returning the same status from main? Yes and no. The Standard says that a return from the initial call to main is equivalent to calling exit. However, a return from main cannot be expected to work if data local to main might be needed during cleanup; A few very ol...
2015-07-20, 1089👍, 0💬

What is the difference between memcpy and memmove?
What is the difference between memcpy and memmove? memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove. It se...
2015-07-16, 1266👍, 0💬

I am trying to port this old program. Why do I get undefined external errors for some library functions?
I am trying to port this old program. Why do I get undefined external errors for some library functions? Some old or semistandard functions have been renamed or replaced over the years; if you need:/you should instead: index use strchr. rindex use strrchr. bcopy use memmove, after interchanging the ...
2015-07-16, 1057👍, 0💬

I keep getting errors due to library functions being undefined, but I am including all the right header files.
I keep getting errors due to library functions being undefined, but I am including all the right header files. In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the exte...
2015-07-14, 1127👍, 0💬

I am still getting errors due to library functions being undefined
I'm still getting errors due to library functions being undefined, even though I'm explicitly requesting the right libraries while linking. Many linkers make one pass over the list of object files and libraries you specify, and extract from libraries only those modules which satisfy references which...
2015-07-14, 1222👍, 0💬

Why is my simple program, which hardly does more than print
Why is my simple program, which hardly does more than print ``Hello, world!'' in a window, compiling to such a huge executable (several hundred K)? Should I #include fewer header files? What you're seeing is the current (poor) state of the ``art'' in library design. As run-time libraries accrete mor...
2015-07-06, 1122👍, 0💬

What does it mean when the linker says that _end is undefined?
What does it mean when the linker says that _end is undefined? That message is a quirk of the old Unix linkers. You get an error about _end being undefined only when other symbols are undefined, too--fix the others, and the error about _end will disappear.
2015-07-06, 1155👍, 0💬

My compiler is complaining that printf is undefined ...
My compiler is complaining that printf is undefined! How can this be? It's the world's most popular C function... Allegedly, there are C compilers for Microsoft Windows which do n ot support printf, on the argument that printf is for printing to old-fashioned terminals, while under Windows the right...
2015-07-03, 1128👍, 0💬

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999? Most computers use base 2 for floating-point numbers as well as for integers, and just as for base 10, not all fractions are representable exactly in base 2. It's well-known that in base 10, a fraction like 1/3 = 0.333...
2015-07-03, 1173👍, 0💬

I am trying to take some square roots
I'm trying to take some square roots, and I've simplified the code down to main() { printf("%f\n", sqrt(144.)); } but I'm still getting crazy numbers. Make sure that you have #included &lt;math.h>, and correctly declared other functions returning double. (Another library function to be careful w...
2015-07-01, 1111👍, 0💬

I am trying to do some simple trig...
I'm trying to do some simple trig, and I am #including &lt;math.h>, but the linker keeps complaining that functions like sin and cos are undefined. Make sure you're actually linking with the math library. For instance, due to a longstanding bug in Unix and Linux systems, you usually need to use ...
2015-07-01, 1157👍, 0💬

My floating-point calculations are acting strangely and giving me different answers on different machines.
My floating-point calculations are acting strangely and giving me different answers on different machines. If the problem isn't that simple, recall that digital computers usually use floating-point formats which provide a close but by no means exact simulation of real number arithmetic. Among other ...
2015-06-29, 1070👍, 0💬

I am sure I've got the trig functions declared correctly, but they are still giving me wrong answers.
I am sure I've got the trig functions declared correctly, but they are still giving me wrong answers. You weren't handing them angles in degrees, were you? C's trig functions (like FORTRAN's and most other languages) accept angles in radians. The conversion from degrees to radians is simple enough: ...
2015-06-29, 1187👍, 0💬

What is a good way to check for ``close enough'' floating-point equality?
What is a good way to check for ``close enough'' floating-point equality? Since the absolute accuracy of floating point values varies, by definition, with their magnitude, the best way of comparing two floating point values is to use an accuracy threshold which is relative to the magnitude of the nu...
2015-06-26, 1311👍, 0💬

How do I round numbers?
How do I round numbers? The simplest and most straightforward way is with code like (int)(x + 0.5) C's floating to integer conversion truncates (discards) the fractional part, so adding 0.5 before truncating arranges that fractions &gt;= 0.5 will be rounded up. (This technique won't work properl...
2015-06-26, 1333👍, 0💬

Why doesn't C have an exponentiation operator?
Why doesn't C have an exponentiation operator? One reason is probably that few processors have a built-in exponentiation instruction. C has a pow function (declared in &lt;math.h>) for performing exponentiation, although explicit multiplication is usually better for small positive integral expon...
2015-06-24, 1240👍, 0💬

The predefined constant M_PI seems to be missing from my machines copy of math.h.
The predefined constant M_PI seems to be missing from my machines copy of math.h. That constant (which is apparently supposed to be the value of pi, accurate to the machine's precision), is not standard; in fact a standard-conforming copy of should not #define a symbol M_PI. If you need pi, you'll h...
2015-06-24, 1237👍, 0💬

How do I set variables to, or test for IEEE NaN?
How do I set variables to, or test for IEEE NaN (``Not a Number'') and other special values? Many systems with high-quality IEEE floating-point implementations provide facilities (e.g. predefined constants, and functions like isnan(), either as nonstandard extensions in &lt;math.h> or perhaps in...
2015-06-21, 1253👍, 0💬

How can I handle floating-point exceptions gracefully?
How can I handle floating-point exceptions gracefully? On many systems, you can define a function matherr which will be called when there are certain floating-point errors, such as errors in the math routines in . You may also be able to use signal to catch SIGFPE.
2015-06-21, 1195👍, 0💬

What is a good way to implement complex numbers in C?
What is a good way to implement complex numbers in C? It is straightforward to define a simple structure and some arithmetic functions to manipulate them. C99 supports complex as a standard type. Here is a tiny example, to give you a feel for it: typedef struct { double real; double imag; } complex;...
2015-06-19, 1358👍, 0💬

<< < 5 6 7 8 9 10 11 12 13 14 15 > >>