arithmetic

MySQL integer unsigned arithmetic problems?

Does MySQL (5.0.45) like to do strange internal typecasts with unsigned maths? I am storing integers unsigned but when selecting basic arithmetic I get outrageous numbers: mysql> create table tt ( a integer unsigned , b integer unsigned , c float ); Query OK, 0 rows affected (0.41 sec) mysql> insert into tt values (215731,216774,1.580...

Sum values of a loop

hi i have this : <% for item in @ventas_ocurridas_en_este_periodo do %> <% @pid = item.producto_id %> <br/> Producto: <%= Producto.find_by_id(@pid) %><br/> Costo de Compra: <%= @costo_de_compra = Producto.find(:all, :conditions => "id = '#{@pid}'").*.costo_de_compra %><br/> Precio de venta: <%= @precio_de_venta = item.precio_de_venta ...

linux script simple arithmetic code

So I'm just working on some simple arithmetic code. Here's what I got: echo "The number should be 2"; declare -i input added input= date +%w let added="input/2" echo "$added" when I run it the output is 4 0 I'm trying to just get 2. What the heck am I doing wrong? ...

A more efficient approach to Verbal arithmetic / Alphametics?

Perhaps most of you know the Send + More = Money. Well, I'm currently learning java and one of the exercises is I have to solve HES + THE = BEST. Now, so far I can/should use if-for-while-do loops, nothing else. Although I'm sure there are different methods to solve it, that's not the point of the exercise I'm going through. I have to b...

Reason for peculiar ordering of division and multiplication in C++

I'm in the middle of porting some c++ code to java, and I keep running across instances where whoever wrote it kept doing the following: double c = (1.0/(a+1.0)*pow(b, a+1.0)); double d = (1./(integral(gamma, dmax)-integral(gamma, dmin)))*(integral(gamma+1, dmax)-integral(gamma+1, dmin)); Instead of: double c = pow(b, a+1.0)/(a+1.0);...

Comparing Doubles in Visual Studio - a standard way to catch this?

Folks, Even experienced programmers write C# code like this sometimes: double x = 2.5; double y = 3; if (x + 0.5 == 3) { // this will never be executed } Basically, it's common knowledge that two doubles (or floats) can never be precisely equal to each other, because of the way the computer handles floating point arithmetic. ...

More simple math help in bash!

In the same thread as this question, I am giving this another shot and ask SO to help address how I should take care of this problem. I'm writing a bash script which needs to perform the following: I have a circle in x and y with radius r. I specify resolution which is the distance between points I'm checking. I need to loop over x and...

Is there a nice way to split an int into two shorts (.NET)?

I think that this is not possible because Int32 has 1 bit sign and have 31 bit of numeric information and Int16 has 1 bit sign and 15 bit of numeric information and this leads to having 2 bit signs and 30 bits of information. If this is true then I cannot have one Int32 into two Int16. Is this true? Thanks in advance. EXTRA INFORMATIO...

How to write an and operation in a SQL statement?

mysql_query("insert into mytable (flow,holderid,amount,operator,ip,product,taskid,comment)values('-1','$memberid','$sum+5','$memberid','$ip','Expertise','$taskid','Publish a problem or task') ")or die(mysql_error()); I get an error about '$sum+5' MySQL doesn't treat it as an and operation, how to resolve this problem? ...

C,Python - different behaviour of the modulo (%) operation

I have found that the same mod operation produces different results depending on what language is being used. In Python: -1 % 10 produces 9 In C it produces -1 ! 1) Which one is the right modulo? 2) How to make mod operation in C to be the same like in Python? Thank you in advance! ...

bizarre javascript arithmetic behavior (yup... to be expected)

ok, I'm writing a little code snippet to get the ISO date-format value for yesterday. code: var dateString = new Date(); var yesterday = dateString.getFullYear(); yesterday += "-"+dateString.getMonth()+1; yesterday += "-"+dateString.getDate()-1; The above code outputs 2009-111-23. It is clearly not treating dateString.getMonth(...

convert integer(long) to double in jsp

Hi, i want to do arithmetic operation on jsp . I am using struts tag lib tag following is the code : <s:set name="value1" value ="%{0.0}" /> <s:set name="value2" value ="%{0.0}" /> <s:set name="percent" value ="%{0.0}" /> <s:iterator> <s:set name="value1" value ="%{#value1+ someIntegerValue1}" /> ...

Float versus Integer arithmetic performance on modern chips

Consider a Viterbi decoder on an additive model. It spends its time doing additions and comparisons. Now, consider two: one with C/C++ float as the data type, and another with int. On modern chips, would you expect int to run significantly faster than float? Or will the wonders of pipelining (and the absence of multiplication and divisio...

How do you get N(N+1) from N+1 + N + 1 + ... + N + 1 + N + 1?

How does: 1 + 2 + ... + N-1 + N + N + N-1 + ... + 2 + 1 --------------------------- N+1 + N+1 + ... + N+1 + N+1 equal N(N + 1)? Shouldn't it be 4N + 4 or 4(N + 1)? ...

Firebird 2.1 TIMESTAMP arithmetic and civil intervals

My understanding is that, in keeping with Interbase v6, Firebird 2.5 does not support the SQL-92 INTERVAL keyword. At least, so suggests this reference and my repeated SQLCODE -104 errors trying to get INTERVALs to work under Firebird's isql(1). How, then, do I account for the irregularities in our civil reckoning of time -- months are...

ArithematicException in Java

In Java, (Number/0) throws an ArithematicException while (Number/0.0) = Infinity. Why is it so.? ...

Is there any difference between DATE_SUB() and using arithmetic operators for datetime calculation?

After I have seen a lot of questions here using the DATE_SUB() or DATE_ADD() functions instead of the arithmetic operators + or -, I was wondering if there was any difference: Quote from the MySQL-manual: Date arithmetic also can be performed using INTERVAL together with the + or - operator: date + INTERVAL expr unit date - INTER...

Python module matrix class that implements Modulo 2 arithmetic?

Hi, I'm looking for a pure Python module that implements a matrix class where the underlying matrix operations are computed in modulo 2 arithmetic as in (x+y)%2 I need to do a lot of basic matrix manipulations ( transpose, multiplication, etc. ). Any help appreciated. Thanks in advance ...

Does F# have generic arithmetic support?

Does F# have the same problem as C# where you can't directly use arithmetic operators with generic T types? Can you write a generic Sum function that would return the sum of any value that supports the arithmetic addition? ...

MySQL query to evaluate aggregate duration of time something is "on"

I have a series of timestamped on/off data in a table, representing on/off states, or the point at which a state "starts" 00:00:00 0 04:00:00 1 08:00:00 0 09:00:00 1 15:00:00 0 20:00:00 1 23:59:59 0 I need to calculate the total duration of (say) the ON state over a 24h period. In this simplified example total du...