## Two Rectangles intersection

I have two rectangles characterized by 4 values each : Left position X, top position Y, width W and height H: X1, Y1, H1, W1 X2, Y2, H2, W2 Rectangles are not rotated, like so: +——————–> X axis | | (X,Y) (X+W, Y) | +————–+ | | | | | | | | | | … Read more

## Math.floor VS Math.trunc JavaScript

Background I am making a function that receives a positive number and then rounds the number to the closest integer bellow it. I have been using Math.floor, but recently I discovered Math.trunc. I am aware that both will return the same value, given a positive number, and that they work in completely different ways. I … Read more

## How much do two rectangles overlap?

I have two rectangles a and b with their sides parallel to the axes of the coordinate system. I have their co-ordinates as x1,y1,x2,y2. I’m trying to determine, not only do they overlap, but HOW MUCH do they overlap? I’m trying to figure out if they’re really the same rectangle give or take a bit … Read more

## How does this bitwise operation check for a power of 2?

I’m looking at some code which should be trivial — but my math is failing me miserably here. Here’s a condition that checks if a number if a power of 2 using the following: if((num != 1) && (num & (num – 1))) { /* make num pow of 2 */ } My question is, … Read more

## Default php function that turns negative numbers in 0

Is there such a thing? for eg \$var = -5; echo thefunction(\$var); // should be 0 \$var = 5; echo thefunction(\$var); // should be 5 Answer Try max(\$var,0), which will have the desired effect. See the manual page for more information. AttributionSource : Link , Question Author : foo , Answer Author : Alexander Gessler

## Cracking short RSA keys

Given the following RSA keys, how does one go about determining what the values of p and q are? Public Key: (10142789312725007, 5) Private Key: (10142789312725007, 8114231289041741) Answer Your teacher gave you: Public Key: (10142789312725007, 5) which means n = 10142789312725007 e = 5 where n is the modulus and e is the public exponent. … Read more

## How do I find the next multiple of 10 of any integer?

Dynamic integer will be any number from 0 to 150. i.e. – number returns 41, need to return 50. If number is 10 need to return 10. Number is 1 need to return 10. Was thinking I could use the ceiling function if I modify the integer as a decimal…? then use ceiling function, and … Read more

## Numerically stable way to compute sqrt((b²*c²) / (1-c²)) for c in [-1, 1]

For some real value b and c in [-1, 1], I need to compute sqrt( (b²*c²) / (1-c²) ) = (|b|*|c|) / sqrt((1-c)*(1+c)) Catastrophic cancellation appears in the denominator when c approaches 1 or -1. The square root probably also does not help. I was wondering if there is a clever trick I can apply … Read more

## Why does Math.min() return -0 from [+0, 0, -0]

I know (-0 === 0) comes out to be true. I am curious to know why -0 < 0 happens? When I run this code in stackoverflow execution context, it returns 0. const arr = [+0, 0, -0]; console.log(Math.min(…arr)); But when I run the same code in the browser console, it returns -0. Why is … Read more

## numpy.sin function in degrees?

I’m working on a problem that has to do with calculating angles of refraction and what not. However, it seems that I’m unable to use the numpy.sin() function in degrees. I have tried to use numpy.degrees() and numpy.rad2deg(). numpy.sin(90) numpy.degrees(numpy.sin(90)) Both return ~ 0.894 and ~ 51.2 respectively. Thanks for your help. Answer You don’t … Read more