Hello! I have a problem where I want to determine the time when an ultrasonic sensor is going to crash into a wall. The problem is this:
Formula:(dist2-dist1)/speed=crashtime; the problem is what if it is stationary? You cannot divide by zero and the arduino program is showing me in the serial monitor that crashtime is -1 and speed is 0.
So don't do it
You can safely assume that it will not crash into anything.
...or that it has already crashed.
In that case, it won't crash into anything else!
I guess that's the answer --> Crashtime = -1
Therefore (at least now after a more or less elastic crash) speed = 0
ec2021
No, the answer is: if speed is zero, don't do the division.
Sorry, I should have used <irony on> ... <\irony off> ;-))
or with other words as -1 is in the past ....
I see that you are still working on that topic
https://forum.arduino.cc/t/gradient-using-ultrasonic-sensor/1099820/8?u=ec2021
As @anon56112670 already mentioned "do not divide by zero". And - as a microcontroller has a quick but simple mind - it is the task of the developer to avoid divisions by zero.
So check the variable you use as a divider whether it is zero or not before the division is performed.
In this example it should be quite obvious that if the object does not move you cannot crash into another static object.
If it does not move you can stop measuring distances and forget about crashtimes ...
Unless you want it to react on something coming in its direction (like driving backwards, start a buzzer or inflate the airbag ... ).
ec2021
const float epsilon = 0.0001;
float crashtime = (dist2 - dist1) / (speed + epsilon);
What if speed is already minus epsilon?
float crashtime = (dist2 - dist1) / (fabsf(speed) + epsilon);
Take a reading of time length zero, as speed is distance over time, so moving zero distance over zero time, speed is 1 because any number divided by itself is equal to 1.
One could also argue that zero divided by any number is zero (almost everywhere).
If 0/0 = 1 we can prove that all numbers are equal ...
1*0 = 2*0 => true, now let's devide both sides by zero
1*0/0 = 2*0/0 => now replace 0/0 by 1
1 = 2
That would ease many math exams of course!
And kill capitalism in a second ...
1*0 = 2*0 => true,
now let's divide both sides by zero
PEMDAS
1*0/0 => 0/0 => 1 or using parenthesis to make it clearer: (1*0) / 0. You must divide the complete expression by 0.
2*0/0 => 0/0 => 1
So, yes it is true.
It is not the same as
1*(0/0) => 1*1
2*(0/0) => 2*1
Should we move this to the bar department or are you serious?
Same for @ec2021...
Off Topic
Do you agree that this is true
a * b / c = (a * b) / c = a * (b / c) = (a / c ) * b
PEMDAS is relevant when you use exponents and also when you mix multiplications, divisions with addition and/or subtraction. It is not relevant when you use multiplications together with divisions. And you do not have to divide the whole expression ( a * b)...
What is irritating in the case of zero as a divisor is that the outcome is different! That's exactly why it is irregular to divide by zero.
Just check it out with other numbers ...
That's talking math; if we talk computing it is often important to check which way is the best to do multiplications and divisions: Based on the data type (integer, unsigned long, float) and the outcome of an interim calculation unexpected results may happen...
Sometimes, in the limit, it is:
Off Topic
Yes that's in the limit (but not reaching zero ... )
Dividing by Zero is similar to
n * ∞ = m*∞ with n = 1 and m = 2
n = (m * ∞) / ∞
n = m * (∞/∞)
n = m
1 = 2