arithmetic

Arithmetic operators and function calling in C

I'm not quite sure why I can't do double a = (double) my_Function(45) / 2048 / 2340 / 90; printf("%.4f",a); // prints out 0.00 But instead I have to use one more variable as: double a = (double) my_Function(45); double b = a / 2048 / 2340 / 90; printf("%.4f",b); // prints out the correct value ...

bit manipulation in java

I have a fragment of bytes in a byte[]. The total size of the array is 4 and I want to convert this into a positive long number. For example if the byte array is having four bytes 101, 110, 10, 10000001 then i want to get the long number represented by binary sequence 00000000 00000000 00000000 00000000 00000101 00000110 00000010 10000...

What is the right data type for calculations in Java

Should we use double or BigDecimal for calculations in Java? How much is the overhead in terms of performance for BigDecimal as compared to double? ...

question about recursive even sum program

hello i need program which returns sum of all even number <= to given n number for example if n=even (let say 10) sum will be 2+4+6+8+10 or if n is odd (let say 13) 2+4+6+8+10+12 please help it should be recursive algorithm i have done myself ...

Help with modulo arithmetic

How do you solve something like 7Xd =(congruent to) 1 mod 40? find the smallest d that satisfies this equation ...

Shift count negative or too big error - correct solution?

I have the following function for reading a big-endian quadword (in a abstract base file I/O class): unsigned long long File::readBigEndQuadWord(){ unsigned long long qT = 0; qT |= readb() << 56; qT |= readb() << 48; qT |= readb() << 40; qT |= readb() << 32; qT |= readb() << 24; qT |= readb() << 16; qT |= readb() << 8; ...

Basic arithmatic in GWT CssResource

I'm looking for a way to do something like this: // style.css @def borderSize '2px'; .style { width: borderSize + 2; height: borderSize + 2; } where the width and height attributes would end up having values of 4px. ...

Java arithmetic int vs. long

Hey all, I've done some searching on google, but I can't seem to find what I'm looking for. I'm trying to find out some detailed information on the way arithmetic works in Java. For example, if you add two longs together is this using the same addition operator that is used when adding two ints together? Also, what is going on under the...

How to test if a coordinate is in a diamond area?

code link: http://jsbin.com/ozoma3/edit codes (can copy/paste to a .html file, and run it) : <HTML> <HEAD> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt; <style> *{margin:0;} #main{ background-image:url('http://clip2net.com/clip/m10494/1277192389-c-811b.png'); width:660px; he...

How to catch the integer division-by-zero exception in C language?

It is known how to catch the float division-by-zero exception with the usage of signal(SIGFPE, handler) but it doesn't catch integer division-by-zero problem even if I setup control word with _control87(0, _MCW_EM ); (MS VC 2010) SubQuestion_1: How to catch integer division-by-zero in C program in Windows without usage of SEH EXCEP...

Atomic arithmetic in Rails

I need to perform some atomic arithmetic in Rails but the only way I've found to do it for single objects is via the crude update_all class method, e.g.: Account.update_all(["debits = debits + ?", amount], :id => id) With collection associations, the update_all class method should be usable as an association method, since the collecti...

VB.NET Bit manipulation: how to extract byte from short?

Given this Short (signed): &Hxxxx I want to: Extract the most right &HxxFF as SByte (signed) Extract the left &H7Fxx as Byte (unsigned) Identify if the most left &H8xxx is positive or negative (bool result) ...

(La)TeX Base 10 fixed point arithmetic

I'm trying to implement decimal arithmetic in (La)TeX. I'm trying to use dimens to store the values. I want the arithmetic to be exact to some (fixed) number of decimal places. If I use 1pt as my base unit, then this fails, because \divide rounds down, so 1pt / 10 gives 0.09999pt. If I use something like 1000sp as my base unit, then ...

BASH: how to perform arithmetic on numbers in a pipe.

I am getting a stream of numbers in a pipe, and would like to perform some operations before passing them on to the next section, but I'm a little lost about how I would go about it without breaking the pipe. for example > echo "1 2 3 4 5" | some command | cat 1 4 9 16 25 > Would you have any ideas on how to make something like this...

Fixed-point multiplication in a known range

Hi, I'm trying to multiply A*B in 16-bit fixed point, while keeping as much accuracy as possible. A is 16-bit in unsigned integer range, B is divided by 1000 and always between 0.001 and 9.999. It's been a while since I dealt with problems like that, so: I know I can just do A*B/1000 after moving to 32-bit variables, then strip back to...

In C#, how do I handle Oracle Float types? Receiving error "Arithmetic operation resulted in an overflow"

I am using a generic approach to receiving a single row from any Oracle table and displaying it in a datagridview, using the code below. But, if the table contains a column of float type and the value has a large number of decimal places, I get "Arithmetic operation resulted in an overflow" at the line: MyReader.GetValues(objCells); ...

error: ambiguous overload for 'operator/'

Hello, I am new to c, and the following is giving me some grief: int i,j,ll,k; double ddim,ddip,ddjm,ddjp,ddlm,ddlp; for(i=1; i<(mx-1); i++){ for(j=1; j<(my-1); j++){ for(ll=1; ll<(mz-1); ll++){ ddim=0.5*k ddip=0.5*k ddjm=0.5*k ddjp=0.5*k ddlm=0.5*k ddlp=0.5*k Wijl(i,j,ll) = ((1.0/h_x)*(ddip) \ ((1.0/h_x)*(ddim)) \ ...

int vs float arithmetic efficiency in Java

Hi all. I'm writing an application that uses Dijkstra algorithm to find minimal paths in the graph. The weights of the nodes and edges in the graph are float numbers, so the algorithm doing many arithmetics on float numbers. Could I gain a running time improve if I convert all weight to ints? Is int arithmetic operations are faster in J...

Basic python arithmetic - division

I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter what I try - I'm getting 0 or 0.0 : >>> count = friends.count() >>> print count 1 >>> per_page = 2 >>> print per_page 2 >>> pages = math.ceil(count/per_pages) >>> p...

Tricky arithmetic or sleight of hand?

Vincent answered Fast Arc Cos algorithm by suggesting this function. float arccos(float x) { x = 1 - (x + 1); return pi * x / 2; } The question is, why x = 1 - (x + 1) and not x = -x? ...