division

What's the fastest way to divide an integer by 3?

int x = n / 3; // <-- make this faster // for instance int a = n * 3; // <-- normal integer multiplication int b = (n << 1) + n; // <-- potentially faster multiplication ...

How to Calculate Recurring Digits?

Given two integers a and b, how would I go about calculating the repeating decimal of a / b? This can be in any language; whatever it's easiest for you to express it in. ...

What's the most performant way to divide two integral values and obtain a floating point quotient in .NET?

Consider the following signature in C#: double Divide(int numerator, int denominator); Is there a performance difference between the following implementations? return (double)numerator / denominator; return numerator / (double)denominator; return (double)numerator / (double)denominator; I'm assuming that both of the above return ...

When is the difference between quotRem and divMod useful?

From the haskell report: The quot, rem, div, and mod class methods satisfy these laws if y is non-zero: (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x quot is integer division truncated toward zero, while the result of div is truncated toward negative infinity. For example: Prelude> (-12) `quot`...

Double value returns 0.

Here's an example: Double d = (1/3); System.out.println(d); This returns 0, not 0.33333... as it should. Does anyone know? ...

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...

Quick divisibility check in ZX81 BASIC

Since many of the Project Euler problems require you to do a divisibility check for quite a number of times, I've been trying to figure out the fastest way to perform this task in ZX81 BASIC. So far I've compared (N/D) to INT(N/D) to check, whether N is dividable by D or not. I have been thinking about doing the test in Z80 machine code...

Shift operation on MC68000

I'm using shift operations to divide a long word in register D3 by three and storing the result in memory address specified by A2: MOVE.L D1, D2, D3, -(SP) CLR.L D5 MOVE.B #1, D4 Loop LSR.L D2, D3 ROXR.L #1,D5 BCS DONE SUBI.B #1, D2 BLT Loop CLR.B D4 ...

How does division work in MIX?

Can someone explain to me how division in MIX (from TAOCP by Knuth) works on a byte-to-byte basis? rA = |-| . . . .0| rX = |+|1235|0|3|1| The memory location 1000 contains |-|0|0|0|2|0|. When you execute the operation DIV 1000 the registers become rA = |+|0|617|?|?| rX = |-|0|0|0|?|1| Now I understand the signs on rA and rX...

Python-style integer division & modulus in C

In Python and Ruby, signed integer division truncates towards negative infinity, and signed integer modulus has the same sign the second operand: >>> (-41) / 3 -14 >>> (-41) % 3 1 However, in C and Java, signed integer division truncates towards 0, and signed integer modulus has the same sign as the first operand: printf("%d\n", (-41...

Check if a number is divisible by 3

Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input "0": (0) output 1 inputs "1,0,0": (4) output 0 inputs "1,1,0,0": (6) out...

Inaccurate division of doubles (Visual C++ 2008)

I have some code to convert a time value returned from QueryPerformanceCounter to a double value in milliseconds, as this is more convenient to count with. The function looks like this: double timeGetExactTime() { LARGE_INTEGER timerPerformanceCounter, timerPerformanceFrequency; QueryPerformanceCounter( if (QueryPerformance...

Java, BigDecimal. Problems with division

Hi. I'm trying to calculate a percentage "factor". That is, given a 20%, convert it into 0.2 (my intention is to later multiply values by that and get the 20% of the values). Anyway, the question is related with this piece of code: public static void main(String[] args) { int roundingMode = BigDecimal.ROUND_FLOOR; BigDecimal hundred...

Assembly mod algorithm on processor with no division operator

This is pretty basic, but I figure it should be on SO for posterity. I need to implement a simple macro that finds the modulo of two numbers on a processor that doesn't have a division operator (think ARM). I could use division by repeated subtraction, but I don't know if this was the most efficient or easiest to work with. Any suggest...

dividing results of two PL/SQL select statements

The results of my two PL/SQL select statements are integers 27 & 50, I want their division (27/50) 0.54 at output...how to do that? I have tried select * from ((select....)/(select ...)) but it does not work!! ...

I need a fast 96-bit on 64-bit specific division algorithm for a fixed-point math library.

I am currently writing a fast 32.32 fixed-point math library. I succeeded at making adding, subtraction and multiplication work correctly, but I am quite stuck at division. A little reminder for those who can't remember: a 32.32 fixed-point number is a number having 32 bits of integer part and 32 bits of fractional part. The best algor...

Signed 64 by 32 integer division

Assuming you have a machine instruction udive that does a special case 64 by 32 unsigned division by taking a (32bit dividend << 32) / 32bit divisor, we can do a full 64 by 32 division using the following: // assume: a / b guaranteed not to overflow a = 64bit dividend, a.h & a.l are hi & lo 32bits respectively b = 32bit divisor q1 = ud...

Change displayable labels for a JSlider?

I have a JSlider with a min of 0 and a max of 10,000. I have the major tick marks set at 1,000. If I were to paint the labels now they would show up as 0, 1000, 2000, 3000, 4000, etc. What I would like to be shown would be 0, 1, 2, 3, 4, 5, etc. What would be a good way to accomplish this task? ...

how can I force division to be floating point in Python?

I have two integer values a and b, but I need their ratio in floating point. I know that a<b and I want to calculate a/b, so if I use integer division I'll always get 0 with a remainder of a. How can I force c to be a floating point number in Python when: c = a / b ...

how to divide my asp.net web page into 2 webpages?

Hi all! I am writing a webPage using asp.net and c#. I want to divide my webpage into 2 columns such as in one I will have buttons that change the view in the other column, without "stepping on" the content of the first column. example: button 1 | :) a picture... button 2 | I tried to use divisions but I think I'm not u...