Hello! I use the same library with CodePlex, but why doesn’t an error or exception occur in the code below?

var speed = 13.9 * 1000 / TimerPeriod * (ts.Seconds + ts.Milliseconds / 1000.0); sel_probe.SegmentLength = 0; var proportion = speed / sel_probe.SegmentLength; // proportion вычисляется, но откуда? 
  • What should? The result should be quite normal - infinity. What are the mistakes here? (If the data were intact, it's another matter) - alexlz
  • one
    @alexlz, there is such a rule in mathematics that it is impossible to divide by zero. Knowing this rule, @alvin asked such a question. In this case, it’s interesting not the output value, but the properties of the compiler ... - AseN
  • one
    @Asen Ecma-334 C # Language Specification 11.1.6 Floating point types - alexlz
  • @Asen, in school mathematics) - Sh4dow

4 answers 4

Perhaps, SegmentLength, when trying to write 0 there, silently writes some non-zero default value.

  • one
    Yes, accessors are used there. - LackOfKnowledge

Try putting a breakpoint on the line.

 var proportion = speed / sel_probe.SegmentLength; 

Open the watch window and look at the value of expressions in it:

 speed sel_probe.SegmentLength speed / sel_probe.SegmentLength 

At the same time find out what type the variable speed has :)

    And something seems to me that everything is much simpler. What is the expected behavior in this case:

     double d = 0; var r = 42/d; 

    DivideByZeroException ? Well then, I'll disappoint you, the result is Infinity , not an exception.

    The variable speed is of type double , and it can be divided by 0 "can" in the sense that there will be no exception.

      Whether sel_probe.SegmentLength is sel_probe.SegmentLength value of "0", most likely the class uses a method that does not allow having / returning a value of 0 for SegmentLength .

      • And in C #, is it possible to overload operators? - LackOfKnowledge
      • but not all. Yes, I did not take into account that the assignment operator cannot be overloaded in C #. corrected. - FLK
      • one
        this is not called a method. this is called getter and setter accessors - LackOfKnowledge