## arithmetic

### Date arithmetic in Unix shell scripts

I need to do date arithmetic in Unix shell scripts that I use to control the execution of third party programs. I'm using a function to increment a day and another to decrement: IncrementaDia(){echo \$1 | awk 'BEGIN { diasDelMes[1] = 31 diasDelMes[2] = 28 diasDelMes[3] = 31 diasDelMes[4] = 30 diasDel...

### Creating a Math library using Generics in C#

Is there any feasible way of using generics to create a Math library that does not depend on the base type chosen to store data? In other words, let's assume I want to write a Fraction class. The fraction can be represented by two ints or two doubles or whatnot. The important thing is that the basic four arithmetic operations are well d...

### Single most effective practice to prevent arithmetic overflow and underflow

What is the single most effective practice to prevent arithmetic overflow and underflow? Some examples that come to mind are: testing based on valid input ranges validation using formal methods use of invariants detection at runtime using language features or libraries (this does not prevent it) ...

### Python - easy way to add N seconds to a datetime.time?

Given a datetime.time value in Python, is there a standard way to add an integer number of seconds to it, so that 11:34:59 + 3 = 11:35:02, for example? These obvious ideas don't work: >>> datetime.time(11, 34, 59) + 3 TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int' >>> datetime.time(11, 34, 59) + datetime.timede...

### Python: unsigned 32 bit bitwise arithmetic

Trying to answer to another post whose solution deals with IP addresses and netmasks, I got stuck with plain bitwise arithmetic. Is there a standard way, in Python, to carry on bitwise AND, OR, XOR, NOT operations assuming that the inputs are "32 bit" (maybe negative) integers or longs, and that the result must be a long in the range [0...

### What's the simplest way of adding one to a binary string in Perl?

I have a variable that contains a 4 byte, network-order IPv4 address (this was created using pack and the integer representation). I have another variable, also a 4 byte network-order, subnet. I'm trying to add them together and add one to get the first IP in the subnet. To get the ASCII representation, I can do inet_ntoa(\$ip&\$netmask...

### Extracting a bit-field from a signed number

Hello, I have signed numbers (2s complement) stored in 32-bit integers, and I want to extract 16-bit fields from them. Is it true that if I extract the low 16 bits from a 32-bit signed number, the result will be correct as long as the original (32-bit) number fits into 16 bits ? For positive numbers it is trivially true, and it seems t...

### Modular arithmetic

I'm new to cryptography and modular arithmetic. So, I'm sure it's a silly question, but I can't help it. How do I calculate a from      pow(a,q) = 1 (mod p), where p and q are known? I don't get the "1 (mod p)" part, it equals to 1, doesn't it? If so, than what is "mod p" about? Is this the same as      pow(a,-q) (mod p) = 1? ...

### Matlab multiplication of 2 vectors error

Hi, i have a small function with 2 vectors alpha = 1 1 1 1 1 1 1 1 1 f_uv = 193 193 194 192 193 193 190 189 191 and i get the error message: "??? Error using ==> mtimes Integers can only be combined with integers of the same class, or scalar doubles." when i try to do this: alphaf_uv = alp...

### Add date without exceeding a month.

Hello. I hope someone could help me on this. I want to add a month to a database date, but I want to prevent two jumping over month on those days at the end. For instance I may have: Jan 31 2009 And I want to get Feb 28 2009 and not March 2 2009 Next date would be March 28 2009 Jun 28 2009 etc. Is there a function that...

### Best way to represent a fraction in Java?

I'm trying to work with fractions in Java. I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6. A naive approach would have me add 2/12 and 6/12 and then reduce. How can I achieve a...

### How do you calculate div and mod of floating point numbers?

In Perl, the % operator seems to assume integers. For instance: sub foo { my \$n1 = shift; my \$n2 = shift; print "perl's mod=" . \$n1 % \$n2, "\n"; my \$res = \$n1 / \$n2; my \$t = int(\$res); print "my div=\$t", "\n"; \$res = \$res - \$t; \$res = \$res * \$n2; print "my mod=" . \$res . "\n\n"; } foo( 3044.952963...

### How can AWK do array arithmetics?

I want to shift away from Excel to Awk. I need basic mathematical operations, such as addition and division, for arrays. For example, arrays A and B are [1, 3, 2] and [2, 1, 2], respectively. How can I get an array [2, 3, 4] from the multiplication between A and B? What about the addition and division between A and B? ...

### What are good "real" programming examples for a beginning programmer?

I've been browsing Bjarne Stroustrup's new introductory programming book, Programming: Principles and Practice Using C++. It's meant for first-year university computer science and engineering students. Early on in the book he works through an interesting extended example of creating a desktop calculator where he ends up implementing an ...

### MySql moving differences?

Suppose I have this code create temporary table somedata (a integer); insert into somedata (a) values (11), (25), (62); --#these values are always increasing select * from somedata; giving this +--+ |a | +--+ |11| |25| |62| +--+ How do I calculate a column of values 'b' where each one is the difference between the value of 'a' in t...

### C++ - Circular array with lower/upper bounds ?

I want to create something similar to a double linked list (but with arrays) that works with lower/upper bounds. A typical circular array would probably look like: next = (current + 1) % count; previous = (current - 1) % count; But what's the mathematical arithmetic to incorporate lower/upper bounds properly into this ? 0 (lower b...

### What to do with Java BigDecimal performance?

I write currency trading applications for living, so I have to work with monetary values (it's a shame that Java still doesn't have decimal float type and has nothing to support arbitrary-precision monetary calculations). "Use BigDecimal!" — you might say. I do. But now I have some code where performance is an issue, and BigDecimal is mo...

### A good uncertainty (interval) arithmetic library?

edited Given that the words "uncertain" and "uncertainty" are fairly ubiquitous, it's hard to Google "uncertainty arithmetic" and get anything immediately helpful. Thus, can anyone suggest a good library of routines, in almost any programming/scripting language, that implements handling of uncertain values, as per this description: ...

### Best way to do binary arithmetic in C?

I am learning C and am trying to write a program that will take 2 value from a user and will check if these strings are binary values (composed of the characters 1 or 0 is how I approached it) before attempting to, according to user selection: Add the two values, Subtract input 2 from input 1, or Multiply the two values. I can implem...

### The precision of the long double output is not correct. What might be wrong?

I have a long double constant that I am setting either as const or not-const. It is longer (40 digits) than the precision of a long double on my test workstation (19 digits). When I print it out, it no longer is displayed at 19 digits of precision, but at 16. Here is the code I am testing: #include <iostream> #include <iomanip> #incl...