Pflichtfeld Active Member Licensed User May 24, 2020 #1 I fill a float-variable in a class with f.i. 1.6 (from a textedit-view) this is the code, I use: B4X: Public Sub getKlingendicke As Float Log(mKlingenDicke) Return mKlingenDicke End Sub Public Sub setKlingendicke (dd As Float) mKlingenDicke=dd End Sub I do not understand what happens now: there are decimals altered, although I do not do any calculations with it. I add a screenshot to make that clear: On the logs-window, you see the altered value. Hoovering over the value shows the exact 1.6 How can I cope with this problem? Edit: Round2 does not change the Situation, everything stayes as before. B4X: Public Sub getKlingendicke As Float Log(Round2(mKlingenDicke,2)) Return Round2(mKlingenDicke,2) 'Seltsamerweise wird da immer was angehäng an den hinteren stellen. End Sub And also strange: When I assign this float-variable back to the text-property of the edittext.view (input type: dezimal_numbers), it is filled correctly with 1.6, ommitting the long decimals. Last edited: May 24, 2020
I fill a float-variable in a class with f.i. 1.6 (from a textedit-view) this is the code, I use: B4X: Public Sub getKlingendicke As Float Log(mKlingenDicke) Return mKlingenDicke End Sub Public Sub setKlingendicke (dd As Float) mKlingenDicke=dd End Sub I do not understand what happens now: there are decimals altered, although I do not do any calculations with it. I add a screenshot to make that clear: On the logs-window, you see the altered value. Hoovering over the value shows the exact 1.6 How can I cope with this problem? Edit: Round2 does not change the Situation, everything stayes as before. B4X: Public Sub getKlingendicke As Float Log(Round2(mKlingenDicke,2)) Return Round2(mKlingenDicke,2) 'Seltsamerweise wird da immer was angehäng an den hinteren stellen. End Sub And also strange: When I assign this float-variable back to the text-property of the edittext.view (input type: dezimal_numbers), it is filled correctly with 1.6, ommitting the long decimals.
agraham Expert Licensed User Longtime User May 24, 2020 #2 This is characteristic of both Float and Double values. They are a binary representation of numbers and so cannot aways represent an exact decimal number. It is the same for all computer languages as it is a feature of the computer architecture. Rounding or Formatting the numbers for display usually achieves what you expect. If you want exact representations see my BigNumbers library. https://www.b4x.com/android/forum/threads/bignumbers-library.9540/ Upvote 0
This is characteristic of both Float and Double values. They are a binary representation of numbers and so cannot aways represent an exact decimal number. It is the same for all computer languages as it is a feature of the computer architecture. Rounding or Formatting the numbers for display usually achieves what you expect. If you want exact representations see my BigNumbers library. https://www.b4x.com/android/forum/threads/bignumbers-library.9540/
Star-Dust Expert Licensed User Longtime User May 24, 2020 #3 Floating-point arithmetic - Wikipedia en.wikipedia.org sign - exp - base Upvote 0
Pflichtfeld Active Member Licensed User May 24, 2020 #4 agraham said: This is characteristic of both Float and Double values. Click to expand... Thank you both, Stardust and Abraham! I changed all my variables to double and now it works fine, as expected Upvote 0
agraham said: This is characteristic of both Float and Double values. Click to expand... Thank you both, Stardust and Abraham! I changed all my variables to double and now it works fine, as expected