arithmetic

F# Checked Arithmetics Scope

F# allows to use checked arithmetics by opening Checked module, which redefines standard operators to be checked operators, for example: open Checked let x = 1 + System.Int32.MaxValue // overflow will result arithmetic overflow exception. But what if I want to use checked arithmetics in some small scope, like C# allows with keyword c...

Assembly: Why are we bothering with registers?

I have a basic question about assembly. Why do we bother doing arithmetic operations only on registers if they can work on memory as well? For example both of the following cause (essentially) the same value to be calculated as an answer: Snippet 1 .data var dd 00000400h .code Start: add var,0000000Bh mov ea...

What is wrong with this arithmetic when using SDCC (Little Endian) Compiler?

I am very new at C programming and I am working on a firmware application for my MCU. This method was working fine when I was using the KEIL compiler (Big Endian) but when I switched to the SDCC compiler (Little Endian) it is not working properly. Can someone please explain what I am doing wrong??? The target device is a Silicon Labs C8...

Execute periodic tasks using a timer with a different interval as intended

Hi, I am forced to execute a periodic task using a timer that is invoked at a different interval as the period I'd like to execute this task. Note that the timeout does not occur 100% accurately; i.e. timeout(javax.ejb.Timer timer) in the code below might be invoked at intervals 100, 98, 105ms etc. So, I came up with this, which is ...

How fast is VB .net compared to native code for arithmetic?

I need to write software that will do a lot of math. Mostly it will be matrix multiplication with integers to compute DCT. How much faster should I expect the code to run in native c as compared to VB .Net? Factor of 2, factor of 10, factor of 1000...? Has someone tried and collected statistics on this? ...

Prolog - Declaring arithmetic clauses

A simple question, how would I go about declaring a clause which would produce the number specified +1 +2 and +3? I have tried: addup(Thenumber,Thenumber+1). addup(Thenumber,Thenumber+2). addup(Thenumber,Thenumber+3). but when I run it with say, Thenumber=5, it just returns 5+1 5+2 5+3. I have tried using 'is' to force it to evaluate ...

C# divide two binary numbers

Is it possible in C# to divide two binary numbers. All I am trying to do is: // get int value into binary format, see below int days = 68; string binary = Convert.ToString(days, 2); // but how do you divide the binary numbers? , what format should be used? 01000100 / 000000100 = 4 Little confused any help would be great. ...

Big O complexity of the basic arithmetic operations

What Big-O complexity have most widespread algorithms for the basic arithmetic operations like multiplication, square root, logarithm, scalar and matrix product? Do exist some exotic algorithms which are the most effective in terms of Big-O complexity but for some reasons not very widespread in practical solutions (e.g. not implemented i...

Pointer incrementing query

I have been looking at this piece of code, and it is not doing what I expect. I have 3 globals. int x, y, *pointer, z; Inside of main I declare them. x = 10; y = 25; pointer = &x; now at this point &x is 0x004A144 &y is 0x004A138 pointer is pointing to 0x004A144 Now when I increment: y = *++pointer; it points to 0x004...

Fastest way to do an unchecked integer addition in VB.Net?

I have a project where I want to have checked arithmetic by default, except for one performance sensitive spot. Unfortunately, VB.Net doesn't have an 'unchecked' block. Ideally the framework would have some sort of integer type with explicitly unchecked arithmetic, but I didn't find anything like that. I did find that expression trees h...

Algorithm for binary arithmetic in Java

On paper, binary arithmetic is simple, but as a beginning programmer, I'm finding it a little difficult to come up with algorithms for the addition, subtraction, multiplication and division of binary numbers. I have two binary numbers stored as strings, assume that any leading zeroes have been dropped. How would I go about performing th...

Why might different computers calculate different arithmetic results in VB.NET?

I have some software written in VB.NET that performs a lot of calculations, mostly extracting jpegs to bitmaps and computing calculations on the pixels like convolutions and matrix multiplication. Different computers are giving me different results despite having identical inputs. What might be the reason? Edit: I can't provide the al...

Modulo operator in Objective-C returns the wrong result

I'm a little freaked out by the results I'm getting when I do modulo arithmetic in Objective-C. -1 % 3 is coming out to be -1, which isn't the right answer: according to my understanding, it should be 2. -2 % 3 is coming out to -2, which also isn't right: it should be 1. Is there another method I should be using besides the % operator ...

How is schoolbook long division an O(n^2) algorithm?

Premise: This Wikipedia page suggests that the computational complexity of "Schoolbook" long division is O(n^2). Deduction: Instead of taking two n-digit numbers, if I take one n-digit number and one m-digit number, then the complexity would be O(n*m). Contradiction: Suppose you divide 100000000 (n digits) b...

Performing Arithmetic on Currency Values in the form of Text using Javascript

Hello all, I am using JQuery to get the contents of a div, which only contains a price in dollars and I would like to add $99 to it, but its text, so when I do the below it won't work. $('#price_' + part[0]).text($('#price_' + part[0]).text() + 99); //Changes the div contents to $10099 - if it the contents was $100 to start with So t...

Is there a way to do 'correct' arithmetical rounding in .NET? / C#

I'm trying to round a number to it's first decimal place and, considering the different MidpointRounding options, that seems to work well. A problem arises though when that number has sunsequent decimal places that would arithmetically affect the rounding. An example: With 0.1, 0.11..0.19 and 0.141..0.44 it works: Math.Round(0.1, 1) ...

BASH Arithmetic Expressions

I had used several ways to do some simple integer arithmetic in BASH (3.2). But I can't figure out the best (preferred) way to do it. result=`expr 1 + 2` result=$(( 1 + 2 )) let "result = 1 + 2" What are the fundamental differences between those expressions? Is there other ways to do the same? Is the use of a tool like bc mandatory f...

How are operators called which affect the first operand instead of 'return' something?

Hello, everyone! I'm talking about operators which not return a value but modify (overwrite) the first operand. Example in pseudo-code: add := return op1 + op2 increment := op1 = op1 + op2 Given this mapping schema: add -> increment subtract -> decrement What could possibly be the names for other operators? multiply, d...

Question on Pointer Arithmetic

Heyy Everybody! I am trying to create a memory management system, so that a user can call myMalloc, a method I created. I have a linked list keeping track of my free memory. My problem is when I am attempting to find the end of a free bit in my linked list. I am attempting to add the size of the memory free in that section (which is i...

simple c# arithmetics. winForms

I'm doing simple divisions in c#, and I am a bit puzzled by its intricacies. Here's some code, and in the comments, the result. (btw, I only compile with 1 line not commented, if you say that I have 5 declarations of the same variable) double result = 2 / 3; //gives 0 double result = Convert.ToDouble(2) / Convert.ToDouble(3); // is good...