EzDevInfo.com

numeric

Numerical analysis in Javascript Numeric Javascript

How to convert a number to string and vice versa in C++

Since this question gets asked about every week, this FAQ might help a lot of users.

  • How to convert an integer to a string in C++

  • how to convert a string into an integer in C++

  • how to convert a floating-point number to a string in C++

  • how to convert a string to a floating-point number in C++


Source: (StackOverflow)

Why SQL Server throws Arithmetic overflow error converting int to data type numeric?

I have an error being thrown by SQL Server Management Studio when running this code:

declare @percentage numeric(3,2)
set @percentage = cast(15 as numeric(3,2))

but when I change numeric declaration to

declare @percentage numeric(4,2)
set @percentage = cast(15 as numeric(4,2))

everything goes fine.

Is there a limitation for numeric data type?


Source: (StackOverflow)

Advertisements

How do get only numeric recaptcha?

I need to show only the numeric reCaptha instead of character strings. Is it possible? If yes, please give the procedure.

Like Google's Street view


Source: (StackOverflow)

converting a number base 10 to base 62 (a-zA-Z0-9)

I have a number in base 10. Is there anyway to translate it to a base 62?

Example:

echo convert(12324324);
// returns Yg3 (fantasy example here)

PHP's base_convert() can convert up to base 36.


Source: (StackOverflow)

Diagnosis of floating-point overflows in C++ programs

I have a situation in which some numerical results (involving floating point arithmetic with double and float) become incorrect for large input sizes, but not for small ones.

In general, I would like to know which tools are available to diagnose conditions such as numerical overflows and problematic loss of precision.

In other words: Is there a tool which complains about overflows etc. the same way valgrind complains about memory errors?


Source: (StackOverflow)

SQLite ORDER BY string containing number starting with 0

as the title states:

I have a select query, which I'm trying to "order by" a field which contains numbers, the thing is this numbers are really strings starting with 0s, so the "order by" is doing this...

...
10
11
12
01
02
03
...

Any thoughts?

EDIT: if I do this: "...ORDER BY (field+1)" I can workaround this, because somehow the string is internally being converted to integer. Is this the a way to "officially" convert it like C's atoi?


Source: (StackOverflow)

Negative numbers are stored as 2's complement in memory, how does the CPU know if it's negative or positive?

-1 can be represented in 4 bit binary as (2's complement) 1111

15 is also represented as 1111.

So, how does CPU differentiate between 15 and -1 when it gets values from memory?


Source: (StackOverflow)

python nan and inf values

Is it possible to set an element of an array to NaN in Python?

Additionally, is it possible to set a variable to +/- infinity? If so, is there any function to check whether a number is infinity or not?


Source: (StackOverflow)

Associativity math: (a + b) + c != a + (b + c)

Recently I was going through an old blog post by Eric Lippert in which, while writing about associativity he mentions that in C#, (a + b) + c is not equivalent to a + (b + c) for certain values of a, b, c.

I am not able to figure out for what types and range of arithmetic values might that hold true and why.


Source: (StackOverflow)

How to create a numeric vector of zero length in R

I wonder how can I create a numeric vector of zero length in R?


Source: (StackOverflow)

Is Java-8's DoubleStream.sum() method stable when run in parallel?

I'm curious about the following construct in Java 8:

double[] doubles = //...
double sum = DoubleStream.of(doubles).parallel().sum();

To cut to the chase:

  • Will the value of sum always be the same, e.g. when run on different computers?

More background...

Floating point arithmetic is lossy and (unlike real-valued arithmetic) is not associative. So unless care is taken in how the work is divided and reassembled, it could lead to non-deterministic results.

I was happy to discover that the sum() method employs Kahan Summation under the hood. This significantly reduces the error, but does still not give precise* results.

In my testing repeated calls appear to return the same result each time, but I'd like to know how stable we can safely assume it is. e.g.:

  1. Stable in all circumstances?
  2. Stable across computers with the same number of cores?
  3. Stable only on a given computer?
  4. Can't depend on it being stable at all?

I'm happy to assume the same JVM version on each computer.

Here's a test I whipped up:

public static void main(String[] args) {
    Random random = new Random(42L);
    for (int j = 1; j < 20; j++) {

        // Stream increases in size and the magnitude of the values at each iteration.
        double[] doubles = generate(random, j*100, j);

        // Like a simple for loop
        double sum1 = DoubleStream.of(doubles).reduce(0, Double::sum); 

        double sum2 = DoubleStream.of(doubles).sum();
        double sum3 = DoubleStream.of(doubles).parallel().sum();

        System.out.println(printStats(doubles, sum1, sum2, sum3));

        // Is the parallel computation stable?
        for (int i = 0; i < 1000; i++) {
            double sum4 = DoubleStream.of(doubles).parallel().sum();
            assert sum4 == sum3;
        }
        Arrays.sort(doubles);
    }
}

/**
 * @param spread When odd, returns a mix of +ve and -ve numbers.
 *               When even, returns only +ve numbers.
 *               Higher values cause a wider spread of magnitudes in the returned values.
 *               Must not be negative.  
 */
private static double[] generate(Random random, int count, int spread) {
    return random.doubles(count).map(x -> Math.pow(4*x-2, spread)).toArray();
}

private static String printStats(double[] doubles, double sum1, double sum2, double sum3) {
    DoubleSummaryStatistics stats = DoubleStream.of(doubles).summaryStatistics();

    return String.format("-----%nMin: %g, Max: %g, Average: %g%n"
            + "Serial difference:   %g%n"
            + "Parallel difference: %g",
            stats.getMin(), stats.getMax(), stats.getAverage(), sum2-sum1, sum3-sum1);
}

When I run this, the first few iterations are:

-----
Min: -1.89188, Max: 1.90414, Average: 0.0541140
Serial difference:   -2.66454e-15
Parallel difference: -2.66454e-15
-----
Min: 0.000113827, Max: 3.99513, Average: 1.17402
Serial difference:   1.70530e-13
Parallel difference: 1.42109e-13
-----
Min: -7.95673, Max: 7.87757, Average: 0.0658356
Serial difference:   0.00000
Parallel difference: -7.10543e-15
-----
Min: 2.53794e-09, Max: 15.8122, Average: 2.96504
Serial difference:   -4.54747e-13
Parallel difference: -6.82121e-13

Notice that while sum2 & sum3 can be assumed to be more accurate than sum1 - they might not be the same as each other!

I seeded Random with 42, so if anyone gets a different result to me, that would immediately prove some something. :-)


* For the curious...

  • Here are some (python) algorithms that give precise results
  • The precise-sum algorithm with the best-sounding performance characteristics I've heard of is given here (ACM subscription or fee required). It takes 5 flops per input, but is written (in C) to exploit instruction-level parallelism and only run 2 - 3 times slower than naive summation, which sounds rather good for a precise result. (c.f. Kahan summation at 4 flops per input)

Source: (StackOverflow)

How to limit iOS keyboard to numeric input

The question pretty much says it all. Is there any way to have the iOS/iPhone keyboard default to numeric input?


Source: (StackOverflow)

Why does the xor operator on two bytes produce an int?

        //key & hash are both byte[]
        int leftPos = 0, rightPos = 31;
        while(leftPos < 16) {
            //possible loss of precision. required: byte, found: int
            key[leftPos] = hash[leftPos] ^ hash[rightPos];
            leftPos++;
            rightPos--;
        }

Why would a bitwise operation on two bytes in Java return an int? I know I could just cast it back to byte, but it seems silly.


Source: (StackOverflow)

angularjs: allows only numbers to be typed into a text box

In angularjs is there any functionality available that allows only numbers to be typed into a text box like


Source: (StackOverflow)

3.days.ago, 2.hours.from_now etc without Rails?

Some book mentioned some gem to decorate numbers with #days, #megabytes, #minutes etc. Is this only in ActiveSupport, or is there a smaller gem that provides this functionality for use in (small) non-rails gems? I want to use this functionality as part of a DSL in a tiny little gem.


Source: (StackOverflow)