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

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

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

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