0

I do know i'm a beginner but this is strange for me.

double x=1/4

gives me 0.0 (exactly like that), but

double x=1/4.0 or x=1.0/4

gives me 0.25.

Is it normal???

DarekK
  • 83
  • 1
  • 8
  • All programmers need to understand the difference between integer and floating point division and how floating point numbers work. – duffymo Aug 21 '16 at 22:13
  • Had no one who could tell me that :) – DarekK Aug 21 '16 at 22:26
  • It's right there in the documentation for the Java / operator. You need to read that stuff when you have problems. You're a beginner - you have a lot to learn. – duffymo Aug 21 '16 at 22:27
  • I've done some Fortran77, Pascal, MathLab...never had any problems of this type :/, so i was surprised. – DarekK Aug 21 '16 at 22:35
  • FORTRAN does integer division that way, too. ALL languages do: http://www.physics.usyd.edu.au/guides/f77errors.html – duffymo Aug 21 '16 at 22:43
  • You may not see it in pascal as that distinguishes between '/' (division for floats) and 'div' (division for integers). So in Pascal x := 1/4; will convert 1 and 4 to floating point before the division and x will be 0.25. But x := 1 div 4; will result in 0.0 being assigned to x. – Penguino Aug 21 '16 at 22:44
  • @duffymo You tell 'em! Make the OP feel bad so he never comes back. This site would be so much better without pesky questions... – nicomp Aug 21 '16 at 23:19
  • Never used div :P for high precission engeneering calculations that i needed, so with my little programing experience that problem has never appeard. :) – DarekK Aug 21 '16 at 23:22
  • 1
    How is providing correct information making anyone feel bad? Please point out the ad hominem portion of my comments. – duffymo Aug 21 '16 at 23:22
  • The first 16 years of my career were spent practicing mechanical engineering for a living. I did finite element analysis for a living. Believe me, high precision engineering calculations and scientific computing depends on knowing what integer division and floating point numbers are. – duffymo Aug 21 '16 at 23:23
  • @duffymo is right...never too much of constructive criticism. Thanks for reply, it's very helpfull. – DarekK Aug 21 '16 at 23:24
  • Thank you for accepting it in the proper spirit, @DarekK. – duffymo Aug 21 '16 at 23:25
  • @duffymo: I did exactly the same for living in last 10 years - never had to worry about precission of calculations...got software for that :P – DarekK Aug 21 '16 at 23:29
  • I had to write a lot of subroutines to plug in and customize behavior for non-linear problems. I always worried about it. – duffymo Aug 21 '16 at 23:30
  • Hehe...i was just a simple structural engeneer :) ...so i rather have used your work. – DarekK Aug 21 '16 at 23:32
  • Nothing simple about structures - great work. I simulated manufacturing problems. I enjoyed it very much. – duffymo Aug 21 '16 at 23:37

0 Answers0