I usually check before any division if the number equals 0,
if (number == 0) {
number = 1;
}
That's what I do.
What do you do? Throw exception?
I usually check before any division if the number equals 0,
if (number == 0) {
number = 1;
}
That's what I do.
What do you do? Throw exception?
Other urls found in this thread:
libdivide.com
gist.github.com
github.com
gnu.org
rohitab.com
twitter.com
Fuck off
of course i throw an exception giving out a wrong answer is absolutely retarded
>Having operations that naturally lend themselves for errors
>Allowing the exact single case of problematic input that fucks shit up
>Not throwing an exception on the exceptional case these operations somehow get to happen anyway despite your safeguards
>Instead, you fucking change variables under the hood unknown to everyone not looking the code
Your kind of pajeet solutions usually result in planes crashing
This is bait
just don't use division, problem solved
Add some epsilon just to be sure
Recursive substraction of integers?
That's even more retarded
use l’hopital’s rule
That only applies to limits dumbo
return dividend
You are doing wrong OP.
When you divide by 0. You need to make the result output as the maximum value possible of the variable type.
E.g. int32 = 2,147,483,647
Any division by 0 results on an infinite result.
But you can't store an infinite number. Thus we round it to the maximum possible.
The limits.h is a good thinkg if you want to do that
I use a Monad.
Use a language with fully dependent types and formally prove at compile time that 0 can never passed as a divisor.
int divide(char* num1, char* num2) {
bool l = num1 / num2;
if(l == exception)
return "fail";
otherwise return true
}
I thought It was common knowledge to have a standard excepted number to be applied.
return INT_MAX or INT_MIN depending on dividend's sign and set errno
or call the zero argument undefined behavior and let it to callee to ensure it doesn't happen
How do you handle cases like a result exist, like sin(0)/0 =1, or 1/(0*0) = +inf (not an indeterminate value)?
__m128i libdivide_s32_do_vector(__m128i numers, const struct libdivide_s32_t *denom) {
uint8_t more = denom->more;
if (more & LIBDIVIDE_S32_SHIFT_PATH) {
uint32_t shifter = more & LIBDIVIDE_32_SHIFT_MASK;
__m128i roundToZeroTweak = _mm_set1_epi32((1U > 31) & roundToZeroTweak);
q = _mm_sra_epi32(q, libdivide_u32_to_m128i(shifter)); // q = q >> shifter
__m128i shiftMask = _mm_set1_epi32((int32_t)((int8_t)more >> 7)); // set all bits of shift mask = to the sign bit of more
q = _mm_sub_epi32(_mm_xor_si128(q, shiftMask), shiftMask); // q = (q ^ shiftMask) - shiftMask;
return q;
}
else {
__m128i q = libdivide_mullhi_s32_flat_vector(numers, _mm_set1_epi32(denom->magic));
if (more & LIBDIVIDE_ADD_MARKER) {
__m128i sign = _mm_set1_epi32((int32_t)(int8_t)more >> 7); // must be arithmetic shift
q = _mm_add_epi32(q, _mm_sub_epi32(_mm_xor_si128(numers, sign), sign)); // q += ((numer ^ sign) - sign);
}
q = _mm_sra_epi32(q, libdivide_u32_to_m128i(more & LIBDIVIDE_32_SHIFT_MASK)); // q >>= shift
q = _mm_add_epi32(q, _mm_srli_epi32(q, 31)); // q += (q < 0)
return q;
}
}
Alrighty then.
see, it's that easy
I cringed, that's subtly wrong, even worse than evidently wrong.
[spoiler]undefined symbol: LIBDIVIDE_S32_SHIFT_PATH[/spoiler]
lul another fucking retard trying to fit in on Jow Forums, jesus christ
what are you talking about? it's defined right here
github.com
var a = 8;
var b = 0;
var c = b && a/b || 'shit';
#include "github.com
yeah nah
>When you divide by 0
I stopped reading here.
Int -> Int -> Maybe Int :)
would work in Go :^)
1 is as close to infinity as 2,147,483,647 though.
Okay user, thanks for your time. Don't call us, we'll call you.
this
Did you stop high school after grade 11 also?
Ask me how I know you failed calculus and limits.
You don't know if you are dealing with negative or positive zero. Source: look at l’hopital’s rule
What is the reciprocal of divide by zero?
yes you can just look at sign bit
Let the SIGFPE kill my process, because that is a bug.
>any number divided by zero is equal to one
huh...so this is the power of a CS degree.
7/0 is zero fucking autist
1. assume the user won't put 0
2. check if 0 is the dividend (if so, ask for root privileges and download and install malware on the computer)
7 / 0 = x
7 = x * 0
7 = 0
done
I have just set a bound like
[Code]
If (foo < 22 && foo > -22){
foo = 23
}
[/Code]
Of course, only when foo should never be zero, and when only the absolute value of foo can be substituted for foo.
Based
>at compile time
In the example there's a runtime distinction whether the divisor is zero or not.
divider / Math.max( 0, number )
return 5; // because I'm so random, Yay!
just let the cpu handle it
There is no such thing as negative zero for integers. Fucking brainlet. The type just doesn't support it. The sign bit will always be positive. That is, unless your dealing with non standard ints. Take a class brainlet.
if foo = 0 then null;
Result is then NULL, which is what you want it to be.
Pro tip: use a case statement if you're writing SQL.
Whoops wrong post. Meant for
How does that solve anything?
number = -3
>>any number divided by zero is equal to one
>huh...so this is the power of a CS degree.
Wut? Any number divided by zero is equal to one??? Where did OP write this?
I guess we have a brainlet here.
??????????????????????
Multiply by infinity you absolute retard
>7/1=1
>There is no such thing as negative zero for integers
What??? C++ int support negative numbers.
That's why there is the unsigned type.
This
(you) now leave
its basic college algebra retard sorry if it went over ur head
>assume a false statement
>show a false statement
it sure is basic, but also kinda meaningless.
> b = b == 0 ? 0.000000001 : b;
> c = a / b;
werks every time
Look at l'hopital's rule. But since Jow Forums is mostly brainlet. Let me explain a little. It is not only the sign of the numerator and determines is the division by zero is infinity or negative infinity but also the sign of the denominator. Since ints don't have a concept of negative zero (only positive zero but floats actually do have negative zero) it is incorrect to say that you can know if the division by zero is negative or positive infinity.
I'm sure you got lost by so just look at pic related. Numerator is ALWAYS positive but the result of the division by zero depends on what side you come from. In other words, negative or positive zero. Which, again, ints do not support but floats do. Fuck off brainlet.
>if (number == 0) {
> number = 1;
you fucking retard
Explaining to the fucking brainles who didn't understand the problem.
OP solution:
if (number == 0) {
number = 1;
}
y = x/number;
so the code above won't give error.
but people could die cause of this.
The good solution:
try {
if(number == 0) {
throw "Division by zero error";
}
y = x/number;
} catch (const char* msg) {
//do something with msg;
}
This is that kind of pajeet shit that I've come to expect from this board. My God god you sicken me.
>> b = b == 0 ? 0.000000001 : b;
>> c = a / b;
>werks every time
Couldn't this throw then overflow?
>>if (number == 0) {
>> number = 1;
>you fucking retard
Totally wrong {number = 1} is the lazy cuck way out.
Should be {number = NULL}
Which returns NULL, which is correct.
What I said
>ints don't have negative zero
What you read
>Cpp doesn't have negative numbers
Hun, when you are dealing with limits the direction the number changes matters. So in a sense there are two types of zeros positive and negative zero. Standard ints have one zero (and it is considered positive) not two unlike floats.
Just don't divide by 0. It's common knowledge that it's just wrong to do so.
You must be retarded, those are not zeros, but rather points the function tends to zero, do you know what that means? There is, and will always only be, one definition of zero.
Floats handle it ok
>Just don't divide by 0. It's common knowledge that it's just wrong to do so.
fucking /thread/ !!!
>make try block
>inside try block check if number is zero
>then throw??
>after try block catch a char* ??
what
Ok now go look at what NULL is smart guy
OP blown the fuck out
How do you do that in superior languages (C, asm)?
That can't be true because there are plenty of rules for using infinity, but divide by zero breaks math like bath salts.
Wouldn't dividing by NULL give error?
Throw a fucking exception, retard.
Yes because NULL is 0 and is either a big retard or hopefully joking
Jesus christ the absolute state of Jow Forums
I literally fell for it in the few minutes since that post.
I went to try it, gives identical results as if 0 was there instead.
This is absolute perfection and is the best way to do division in pascal.
Can anyone with a CS degree give us the proper answer??
I'm full of those "self-learning" pajeet tier programmers from here.
if (b == 0)
print("division by zero")
else
return a/b
Wow, so hard.
What does it return after printing division by zero?
epic simply epic :)
>his programming language doesn't support "infinity" as a number value
"infinity" is not the correct answer
lrn2maths
0/2 should not be an error, but in your program it is. Also calling exit from inside a trivial function is evil.
#define NULL 0
>printf
KEK!!!!
Not using the superior std::cout
>if (a == 0
Absolute failure at math.
gnu.org
rohitab.com
now
you can also just write your own exception handler you lazy fuck who is unable to use the "superior" languages
Easy
I don't divide by zero
Hey user, type f(x) = 1/x into a graphing calculator and look at what happens when x approaches zero from the left to see why you're wrong
if(number == 0){
println("I'm sorry Dave, I'm afraid I can't do that.");
}