Tag: c

51 votes
21 answers
735 views
Is there a standard sign function (signum, sgn) in C/C++?

I want a function that returns -1 for negative numbers and +1 for positive numbers. http://en.wikipedia.org/wi

avatar
Dorinda
0 1 0 9
asked 07 September, 2021
62 votes
12 answers
2.4K views
JavaScript math, round to two decimal places

I have the following JavaScript syntax: var discount = Math.round(100 - (price / listprice) * 100)

avatar
Lashawna
0 1 0 9
asked 07 September, 2021
63 votes
26 answers
2.7K views
How to check if a number is a power of 2

Today I needed a simple algorithm for checking if a number is a power of 2. The algorithm needs to be: Simpl

avatar
Majorie
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
793 views
Divide a number by 3 without using *, /, +, -, % operators

How would you divide a number by 3 without using *, /, +, -, %, operators? The number may be signed or unsig

avatar
Bryant
0 1 0 9
asked 07 September, 2021
57 votes
6 answers
2.2K views
How do I determine whether my calculation of pi is accurate?

I was trying various methods to implement a program that gives the digits of pi sequentially. I tried the Tayl

avatar
Hilario
0 1 0 9
asked 07 September, 2021
61 votes
28 answers
1.6K views
Understanding "randomness"

I can't get my head around this, which is more random? rand() OR: rand() * rand() I´m finding it a

avatar
Thurman
0 1 0 9
asked 07 September, 2021
67 votes
21 answers
1.8K views
What is JavaScript's highest integer value that a number can go to without losing precision?

Is this defined by the language? Is there a defined maximum? Is it different in different browsers?

avatar
Keith
0 1 0 9
asked 07 September, 2021
52 votes
18 answers
1.7K views
How to perform an integer division, and separately get the remainder, in JavaScript?

In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder?

avatar
Dusty
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
436 views
Determine Whether Two Date Ranges Overlap

Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges ove

avatar
Juliane
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
1K views
Fastest way to determine if an integer's square root is an integer

I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is

avatar
Margo
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
1.4K views
Is floating point math broken?

Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004

avatar
Dwain
0 1 0 9
asked 07 September, 2021
54 votes
4 answers
2.4K views
Is there an R function for finding the index of an element in a vector?

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x

avatar
Basil
0 1 0 9
asked 07 September, 2021
54 votes
6 answers
2.5K views
How to find the length of a string in R

How to find the length of a string (number of characters in a string) without splitting it in R? I know how to

avatar
Nereida
0 1 0 9
asked 07 September, 2021
60 votes
15 answers
1.8K views
How can I trim leading and trailing white space?

I am having some troubles with leading and trailing white space in a data.frame. For example, I like to take a

avatar
Daniel
0 1 0 9
asked 07 September, 2021
64 votes
29 answers
2.6K views
Elegant way to check for missing packages and install them?

I seem to be sharing a lot of code with coauthors these days. Many of them are novice/intermediate R users and

avatar
Dwain
0 1 0 9
asked 07 September, 2021
54 votes
12 answers
1.9K views
How can two strings be concatenated?

How can I concatenate (merge, combine) two values? For example I have: tmp = cbind("GAD", "AB") tmp # [,

avatar
Alphonso
0 1 0 9
asked 07 September, 2021
61 votes
6 answers
1.9K views
How do I install an R package from source?

A friend sent me along this great tutorial on webscraping NYtimes with R. I would really love to try it. Howev

avatar
Louetta
0 1 0 9
asked 07 September, 2021
52 votes
5 answers
2.7K views
How to write trycatch in R

I want to write trycatch code to deal with error in downloading from the web. url <- c( "http://stat.e

avatar
Brianne
0 1 0 9
asked 07 September, 2021
57 votes
30 answers
2.1K views
How to find the statistical mode?

In R, mean() and median() are standard functions which do what you'd expect. mode() tells you the intern

avatar
Corrin
0 1 0 9
asked 07 September, 2021
57 votes
20 answers
1.6K views
Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,5

avatar
Rudy
0 1 0 9
asked 07 September, 2021