Floating Point Number Divided By 0 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the java divide by zero infinity company Business Learn more about hiring developers or posting ads with us Stack Overflow
Float Divide By Zero C++
Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 floating point divide by zero c++ million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in floating point division by zero error Java up vote 37 down vote favorite 9 The following statement throws java.lang.ArithmeticException: / by zero as obvious. System.out.println(0/0); because the literal 0 is considered to be an int literal and divide by zero is not allowed in integer arithmetic. The following case however doesn't throw any exception like java.lang.ArithmeticException: / by zero. int a = 0; double b = 6.199; System.out.println((b/a)); It displays Infinity. The following statement produces NaN (Not a Number)
Floating Point Division By Zero Pdf
with no exception. System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic. In this case, both of the operands are considered to be double. Similarly, the following statements don't throw any exception. double div1 = 0D/0; //or 0D/0D double div2 = 0/0D; //or 0D/0D System.out.printf("div1 = %s : div2 = %s%n", div1, div2); System.out.printf("div1 == div2 : %b%n", div1 == div2); System.out.printf("div1 == div1 : %b%n", div1 == div1); System.out.printf("div2 == div2 : %b%n", div2 == div2); System.out.printf("Double.NaN == Double.NaN : %b%n", Double.NaN == Double.NaN); System.out.printf("Float.NaN == Float.NaN : %b%n", Float.NaN == Float.NaN); They produce the following output. div1 = NaN : div2 = NaN div1 == div2 : false div1 == div1 : false div2 == div2 : false Double.NaN == Double.NaN : false Float.NaN == Float.NaN : false They all return false. Why is this operation (division by zero) allowed with floating point or double precision numbers? By the way, I can understand that floating point numbers (double precision numbers) have their values that represent positive infinity, negative infinity, not a number (NaN)... java arithmeticexception dividebyzeroexception share|improve this question edited Oct 18 '12 at 17:29 asked Oct 18 '12 at 12:05 Tiny 5,23252190349 In the first case, since you are using an int, the promotion might result in something like 0.00..0
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site java divide by zero exception handling About Us Learn more about Stack Overflow the company Business Learn more about java divide by 0 exception hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
0/0.0 In Java
the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Divide by Zero: Infinite, http://stackoverflow.com/questions/12954193/why-does-division-by-zero-with-floating-point-or-double-precision-numbers-not NaN, or Zero Division Error? [closed] up vote 4 down vote favorite 2 Why isn't 1/0 == Inf in every language? Is that not the most mathematically correct response? All the languages I'm familiar with are capable of expressing both Infinite and NaN values, so why would they choose to throw an error or return NaN instead? Is it just to make life harder for http://stackoverflow.com/questions/6563788/divide-by-zero-infinite-nan-or-zero-division-error scientific application developers? ;-) Update: We should maybe close this question because I incorrectly thought that 1f/0f == Float.NaN in Java. But I was wrong: it does correctly return Float.Infinity. That was my main confusion; the fact that some languages throw errors instead is understandable, so long as no language returns NaN. math programming-languages divide-by-zero share|improve this question edited Jul 3 '11 at 17:30 asked Jul 3 '11 at 15:34 Neil Traft 9,599104256 closed as not constructive by Neil Traft, woodchips, Tim Post♦ Jul 3 '11 at 20:03 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question. 1 1/0 always returns Infinite, and possibly raises a floating point exception. 0/0 returns NaN, and possibly raises a floating point exception. –ninjalj Jul 3 '11 at 15:37 2 @Tim Norde
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User https://ubuntuforums.org/showthread.php?t=1398677 Documentation Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Best way to check for divide by 0 in C++? Having an Issue With Posting ? Do you want to help us debug the posting issues ? < by zero is the place to report it, thanks ! Results 1 to 8 of 8 Thread: Best way to check for divide by 0 in C++? Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode February 5th, 2010 #1 SNYP40A1 View Profile View Forum Posts Private Message Quad Shot of Ubuntu Join Date Mar 2007 Beans 462 Best way to check for divide by zero divide by 0 in C++? If you divide by 0 in C++, the program will crash...at least in gcc. What's the best way to check for zero before dividing? Will this work: double den; if(den != 0) I know this probably sounds like a stupid question (and maybe it is), but I'm thinking that floating point rounding errors might come into play here. What's the best way to check a denominator for division to avoid getting an exception? Adv Reply February 5th, 2010 #2 NovaAesa View Profile View Forum Posts Private Message Extra Foam Sugar Free Ubuntu Join Date Aug 2007 Location Novocastria, Australia Beans 754 DistroUbuntu 9.04 Jaunty Jackalope Re: Best way to check for divide by 0 in C++? I would go with setting up the logic of the program such that the denominator is never actually zero. If it's user input, that would involve filtering out values that will lead to a denominator being zero. Most algorithms I have come across have divide by zero mapping directly to a case where the input is invalid. With floating point arithmetic, when comparing two numbers it is best to do it within a certain tolerance. You can obviously change the value of e depending on what precision numbers you are using. Pseudocod
by David Goldberg, published in the March, 1991 issue of Computing Surveys. Copyright 1991, Association for Computing Machinery, Inc., reprinted by permission. Abstract Floating-point arithmetic is considered an esoteric subject by many people. This is rather surprising because floating-point is ubiquitous in computer systems. Almost every language has a floating-point datatype; computers from PCs to supercomputers have floating-point accelerators; most compilers will be called upon to compile floating-point algorithms from time to time; and virtually every operating system must respond to floating-point exceptions such as overflow. This paper presents a tutorial on those aspects of floating-point that have a direct impact on designers of computer systems. It begins with background on floating-point representation and rounding error, continues with a discussion of the IEEE floating-point standard, and concludes with numerous examples of how computer builders can better support floating-point. Categories and Subject Descriptors: (Primary) C.0 [Computer Systems Organization]: General -- instruction set design; D.3.4 [Programming Languages]: Processors -- compilers, optimization; G.1.0 [Numerical Analysis]: General -- computer arithmetic, error analysis, numerical algorithms (Secondary) D.2.1 [Software Engineering]: Requirements/Specifications -- languages; D.3.4 Programming Languages]: Formal Definitions and Theory -- semantics; D.4.1 Operating Systems]: Process Management -- synchronization. General Terms: Algorithms, Design, Languages Additional Key Words and Phrases: Denormalized number, exception, floating-point, floating-point standard, gradual underflow, guard digit, NaN, overflow, relative error, rounding error, rounding mode, ulp, underflow. Introduction Builders of computer systems often need information about floating-point arithmetic. There are, however, remarkably few sources of detailed information about it. One of the few books on the subject, Floating-Point Computation by Pat Sterbenz, is long out of print. Thi