absolute-value

How do I get the absolute value of an integer without using Math.abs?

风格不统一 提交于 2019-12-06 09:18:47
How do I get the absolute value of a number without using math.abs? This is what I have so far: function absVal(integer) { var abs = integer * integer; return abs^2; } You can use the conditional operator and the unary negation operator : function absVal(integer) { return integer < 0 ? -integer : integer; } You can also use >> (Sign-propagating right shift) function absVal(integer) { return (integer ^ (integer >> 31)) - (integer >> 31);; } Note: this will work only with integer Since the absolute value of a number is "how far the number is from zero", a negative number can just be "flipped"

How can I find the difference between 2 values in C#?

怎甘沉沦 提交于 2019-12-06 07:19:48
I am working with an Oscillator that fluctuates between 10.000000 and -10.000000 The value changes say every 5 minutes. I want to find the difference between the current value and the value of 5 minutes ago. Here is my logic. 1 bar ago (1BA)= -.2 Current bar (CB) = .3 Wouldn't I get a value of 1 if I did something like: Abs(CB) - Abs(1BA) = .3 - .2 = 1 Whereas: Abs(CB- 1BA) = .3 - -.2 = 5 I want to simply calculate the difference between the move of the oscillator from one time frame to another. Am I applying the Abs with the right logic in mind? Here is my actual code, please assume my method

abs 'implicit declaration…' error after including math.h

倾然丶 夕夏残阳落幕 提交于 2019-12-05 16:05:22
问题 I used the abs() function and I added #include <math.h> at the top of code. But I keep getting this error: hello.c:20:11: warning: implicit declaration of function 'abs' is invalid in C99 [-Wimplicit-function-declaration] int a = abs(arrOfHour[i] - hour) * 60 + minute; ^ I'm using LLVM compiler. Why does this error occurs even though I have included math.h ? 回答1: I'm going to quote straight from the docs : "Prototypes for abs, labs and llabs are in stdlib.h" As a rule of thumb the

On the std::abs function

社会主义新天地 提交于 2019-12-03 08:37:56
问题 Is the std::abs() function well defined for ALL arithmetic types in C++11 and will return |x| with no problem of approximation? A weird thing is that with g++4.7, std::abs(char) , std::abs(short int) , std::abs(int) , std::abs(long int) and std::abs(long long int) seem to return a double (on the contrary of : http://en.cppreference.com/w/cpp/numeric/math/abs). And if the number is casted to a double, we could have some approximation error for very large number (like -9223372036854775806LL = 2

What is different about C++ math.h abs() compared to my abs()

断了今生、忘了曾经 提交于 2019-12-03 05:17:52
I am currently writing some glsl like vector math classes in C++, and I just implemented an abs() function like this: template<class T> static inline T abs(T _a) { return _a < 0 ? -_a : _a; } I compared its speed to the default C++ abs from math.h like this: clock_t begin = clock(); for(int i=0; i<10000000; ++i) { float a = abs(-1.25); }; clock_t end = clock(); unsigned long time1 = (unsigned long)((float)(end-begin) / ((float)CLOCKS_PER_SEC/1000.0)); begin = clock(); for(int i=0; i<10000000; ++i) { float a = myMath::abs(-1.25); }; end = clock(); unsigned long time2 = (unsigned long)((float)

On the std::abs function

只谈情不闲聊 提交于 2019-12-02 22:23:18
Is the std::abs() function well defined for ALL arithmetic types in C++11 and will return |x| with no problem of approximation? A weird thing is that with g++4.7, std::abs(char) , std::abs(short int) , std::abs(int) , std::abs(long int) and std::abs(long long int) seem to return a double (on the contrary of : http://en.cppreference.com/w/cpp/numeric/math/abs ). And if the number is casted to a double, we could have some approximation error for very large number (like -9223372036854775806LL = 2^63-3 ). So do I have the guarantee that std::abs(x) will always return |x| for all arithmetic types ?

How does this function compute the absolute value of a float through a NOT and AND operation?

佐手、 提交于 2019-12-01 18:00:26
I am trying to understand how the following code snippet works. This program uses SIMD vector instructions (Intel SSE) to calculate the absolute value of 4 floats (so, basically, a vectorized "fabs()" function). Here is the snippet: #include <iostream> #include "xmmintrin.h" template <typename T> struct alignas(16) sse_t { T data[16/sizeof(T)]; }; int main() { sse_t<float> x; x.data[0] = -4.; x.data[1] = -20.; x.data[2] = 15.; x.data[3] = -143.; __m128 a = _mm_set_ps1(-0.0); // ??? __m128 xv = _mm_load_ps(x.data); xv = _mm_andnot_ps(a,xv); // <-- Computes absolute value sse_t<float> result;

How does this function compute the absolute value of a float through a NOT and AND operation?

半腔热情 提交于 2019-12-01 02:08:55
问题 I am trying to understand how the following code snippet works. This program uses SIMD vector instructions (Intel SSE) to calculate the absolute value of 4 floats (so, basically, a vectorized "fabs()" function). Here is the snippet: #include <iostream> #include "xmmintrin.h" template <typename T> struct alignas(16) sse_t { T data[16/sizeof(T)]; }; int main() { sse_t<float> x; x.data[0] = -4.; x.data[1] = -20.; x.data[2] = 15.; x.data[3] = -143.; __m128 a = _mm_set_ps1(-0.0); // ??? __m128 xv

Unsigned int to signed in php

微笑、不失礼 提交于 2019-11-30 21:29:12
It appears, that in 32bit OS ip2long returns signed int, and in 64bit OS unsigned int is returned. My application is working on 10 servers, and some are 32bit and some are 64bit, so I need all them to work same way. In PHP documentation there is a trick to make that result always unsigned, but since I got my database already full of data, I want to have it signed. So how to change an unsigned int into a signed one in PHP? PHP does not support unsigned integers as a type, but what you can do is simply turn the result of ip2long into an unsigned int string by having sprintf interpret the value

Unsigned int to signed in php

怎甘沉沦 提交于 2019-11-30 05:36:29
问题 It appears, that in 32bit OS ip2long returns signed int, and in 64bit OS unsigned int is returned. My application is working on 10 servers, and some are 32bit and some are 64bit, so I need all them to work same way. In PHP documentation there is a trick to make that result always unsigned, but since I got my database already full of data, I want to have it signed. So how to change an unsigned int into a signed one in PHP? 回答1: PHP does not support unsigned integers as a type, but what you can