Author Topic: 4 / 6 = .67  (Read 8483 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 4 / 6 = .67
« Reply #15 on: February 02, 2020, 11:47:09 am »
Hi Krovit,

Using your method ... but it only returns the decimal places ... I need the whole number as well.

5/3 = .67  this is great .. but I want the 1.67.

Thks,

Mike


Maybe if you see the actual numbers in the formula Steve gave you?
Code: QB64: [Select]
  1. PRINT INT(4 / 6 * 100 + .5) / 100
  2. PRINT INT(5 / 3 * 100 + .5) / 100
  3.  

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: 4 / 6 = .67
« Reply #16 on: February 02, 2020, 03:20:50 pm »
This is way without string conversions. Function parameters are: decimal number, lenght

Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6.  
  7. FUNCTION LNUM! (num!, lenght)
  8.     '1.2345, 2  =>  1.23
  9.     integernum = num \ 1
  10.     decimalnum = num / 1 - num! \ 1
  11.     n = 10 ^ lenght
  12.     newdecimalnum = decimalnum * n
  13.     newdecimalnum = INT(newdecimalnum)  'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  14.     newdecimalnum = newdecimalnum / n
  15.     LNUM = integernum + newdecimalnum
  16.  
  17.  

Hey -- thanks for that.  That's really clear.  I like code that I can understand!

Malcolm

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 4 / 6 = .67
« Reply #17 on: February 02, 2020, 04:00:21 pm »
Thank You, MWheatley. I will never use the insanity in one row. Source code clearity is much better than small source code size.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: 4 / 6 = .67
« Reply #18 on: February 03, 2020, 04:26:12 am »
Thank you everyone.

Mike

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: 4 / 6 = .67
« Reply #19 on: February 03, 2020, 10:36:47 am »
I added the following to the code and it seems it will not produce a decimal place beyond 6. I'm not sure why that would be.

Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6. PRINT "1 decimal place   ";
  7. PRINT LNUM(22 / 7, 1)
  8. PRINT "2 decimal places  ";
  9. PRINT LNUM(22 / 7, 2)
  10. PRINT "3 decimal places  ";
  11. PRINT LNUM(22 / 7, 3)
  12. PRINT "4 decimal places  ";
  13. PRINT LNUM(22 / 7, 4)
  14. PRINT "5 decimal places  ";
  15. PRINT LNUM(22 / 7, 5)
  16. PRINT "6 decimal places  ";
  17. PRINT LNUM(22 / 7, 6)
  18. PRINT "7 decimal places  ";
  19. PRINT LNUM(22 / 7, 7)
  20. PRINT "8 decimal places  ";
  21. PRINT LNUM(22 / 7, 8)
  22. PRINT "9 decimal places  ";
  23. PRINT LNUM(22 / 7, 9)
  24. PRINT "10 decimal places ";
  25. PRINT LNUM(22 / 7, 10)
  26.  
  27.  
  28.  
  29.  
  30. FUNCTION LNUM! (num!, lenght)
  31.     '1.2345, 2  =>  1.23
  32.     integernum = num \ 1
  33.     decimalnum = num / 1 - num! \ 1
  34.     n = 10 ^ lenght
  35.     newdecimalnum = decimalnum * n
  36.     newdecimalnum = INT(newdecimalnum) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  37.     newdecimalnum = newdecimalnum / n
  38.     LNUM = integernum + newdecimalnum

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 4 / 6 = .67
« Reply #20 on: February 03, 2020, 11:47:46 am »
I added the following to the code and it seems it will not produce a decimal place beyond 6. I'm not sure why that would be.

Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6. PRINT "1 decimal place   ";
  7. PRINT LNUM(22 / 7, 1)
  8. PRINT "2 decimal places  ";
  9. PRINT LNUM(22 / 7, 2)
  10. PRINT "3 decimal places  ";
  11. PRINT LNUM(22 / 7, 3)
  12. PRINT "4 decimal places  ";
  13. PRINT LNUM(22 / 7, 4)
  14. PRINT "5 decimal places  ";
  15. PRINT LNUM(22 / 7, 5)
  16. PRINT "6 decimal places  ";
  17. PRINT LNUM(22 / 7, 6)
  18. PRINT "7 decimal places  ";
  19. PRINT LNUM(22 / 7, 7)
  20. PRINT "8 decimal places  ";
  21. PRINT LNUM(22 / 7, 8)
  22. PRINT "9 decimal places  ";
  23. PRINT LNUM(22 / 7, 9)
  24. PRINT "10 decimal places ";
  25. PRINT LNUM(22 / 7, 10)
  26.  
  27.  
  28.  
  29.  
  30. FUNCTION LNUM! (num!, lenght)
  31.     '1.2345, 2  =>  1.23
  32.     integernum = num \ 1
  33.     decimalnum = num / 1 - num! \ 1
  34.     n = 10 ^ lenght
  35.     newdecimalnum = decimalnum * n
  36.     newdecimalnum = INT(newdecimalnum) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  37.     newdecimalnum = newdecimalnum / n
  38.     LNUM = integernum + newdecimalnum

Maybe _FLOAT instead of single will buy more decimal places.

yep!
Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6. PRINT "1 decimal place   ";
  7. PRINT LNUM(22 / 7, 1)
  8. PRINT "2 decimal places  ";
  9. PRINT LNUM(22 / 7, 2)
  10. PRINT "3 decimal places  ";
  11. PRINT LNUM(22 / 7, 3)
  12. PRINT "4 decimal places  ";
  13. PRINT LNUM(22 / 7, 4)
  14. PRINT "5 decimal places  ";
  15. PRINT LNUM(22 / 7, 5)
  16. PRINT "6 decimal places  ";
  17. PRINT LNUM(22 / 7, 6)
  18. PRINT "7 decimal places  ";
  19. PRINT LNUM(22 / 7, 7)
  20. PRINT "8 decimal places  ";
  21. PRINT LNUM(22 / 7, 8)
  22. PRINT "9 decimal places  ";
  23. PRINT LNUM(22 / 7, 9)
  24. PRINT "10 decimal places ";
  25. PRINT LNUM(22 / 7, 10)
  26.  
  27.  
  28.  
  29.  
  30. 'FUNCTION LNUM! (num!, lenght)
  31. '    '1.2345, 2  =>  1.23
  32. '    integernum = num \ 1
  33. '    decimalnum = num / 1 - num! \ 1
  34. '    n = 10 ^ lenght
  35. '    newdecimalnum = decimalnum * n
  36. '    newdecimalnum = INT(newdecimalnum) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  37. '    newdecimalnum = newdecimalnum / n
  38. '    LNUM = integernum + newdecimalnum
  39. 'END FUNCTION
  40.  
  41.  
  42. FUNCTION LNUM## (num##, leng)
  43.     LNUM## = INT(num## * 10 ^ leng + .5) / 10 ^ leng
  44.  
  45.  
« Last Edit: February 03, 2020, 12:02:34 pm by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 4 / 6 = .67
« Reply #21 on: February 03, 2020, 11:54:15 am »
Its because it is SINGLE precision. Try DOUBLE, or FLOAT:

Double:

Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6. PRINT "1 decimal place   ";
  7. PRINT LNUM(22 / 7, 1)
  8. PRINT "2 decimal places  ";
  9. PRINT LNUM(22 / 7, 2)
  10. PRINT "3 decimal places  ";
  11. PRINT LNUM(22 / 7, 3)
  12. PRINT "4 decimal places  ";
  13. PRINT LNUM(22 / 7, 4)
  14. PRINT "5 decimal places  ";
  15. PRINT LNUM(22 / 7, 5)
  16. PRINT "6 decimal places  ";
  17. PRINT LNUM(22 / 7, 6)
  18. PRINT "7 decimal places  ";
  19. PRINT LNUM(22 / 7, 7)
  20. PRINT "8 decimal places  ";
  21. PRINT LNUM(22 / 7, 8)
  22. PRINT "9 decimal places  ";
  23. PRINT LNUM(22 / 7, 9)
  24. PRINT "10 decimal places ";
  25. PRINT LNUM(22 / 7, 10)
  26.  
  27.  
  28.  
  29.  
  30. FUNCTION LNUM# (num#, lenght)
  31.     '1.2345, 2  =>  1.23
  32.     integernum# = num# \ 1
  33.     decimalnum# = num# / 1 - num# \ 1
  34.     n = 10 ^ lenght
  35.     newdecimalnum# = decimalnum# * n
  36.     newdecimalnum# = INT(newdecimalnum#) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  37.     newdecimalnum# = newdecimalnum# / n
  38.     LNUM# = integernum# + newdecimalnum#
  39.  
  40.  

or FLOAT:

Code: QB64: [Select]
  1.  
  2. PRINT LNUM(0.0001, 4)
  3. PRINT 22 / 7
  4. PRINT LNUM(22 / 7, 1)
  5. PRINT LNUM(22 / 7, 5)
  6.  
  7. PRINT "1 decimal place   ";
  8. PRINT LNUM(22 / 7, 1)
  9. PRINT "2 decimal places  ";
  10. PRINT LNUM(22 / 7, 2)
  11. PRINT "3 decimal places  ";
  12. PRINT LNUM(22 / 7, 3)
  13. PRINT "4 decimal places  ";
  14. PRINT LNUM(22 / 7, 4)
  15. PRINT "5 decimal places  ";
  16. PRINT LNUM(22 / 7, 5)
  17. PRINT "6 decimal places  ";
  18. PRINT LNUM(22 / 7, 6)
  19. PRINT "7 decimal places  ";
  20. PRINT LNUM(22 / 7, 7)
  21. PRINT "8 decimal places  ";
  22. PRINT LNUM(22 / 7, 8)
  23. PRINT "9 decimal places  ";
  24. PRINT LNUM(22 / 7, 9)
  25. PRINT "10 decimal places ";
  26. PRINT LNUM(22 / 7, 10)
  27.  
  28.  
  29.  
  30.  
  31. FUNCTION LNUM## (num##, lenght)
  32.     '1.2345, 2  =>  1.23
  33.     integernum## = num## \ 1
  34.     decimalnum## = num## / 1 - num## \ 1
  35.     n = 10 ^ lenght
  36.     newdecimalnum## = decimalnum## * n
  37.     newdecimalnum## = INT(newdecimalnum##) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  38.     newdecimalnum## = newdecimalnum## / n
  39.     LNUM## = integernum## + newdecimalnum##
  40.  
  41.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 4 / 6 = .67
« Reply #22 on: February 03, 2020, 12:05:24 pm »
Its because it is SINGLE precision. Try DOUBLE, or FLOAT:

Double:

Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6. PRINT "1 decimal place   ";
  7. PRINT LNUM(22 / 7, 1)
  8. PRINT "2 decimal places  ";
  9. PRINT LNUM(22 / 7, 2)
  10. PRINT "3 decimal places  ";
  11. PRINT LNUM(22 / 7, 3)
  12. PRINT "4 decimal places  ";
  13. PRINT LNUM(22 / 7, 4)
  14. PRINT "5 decimal places  ";
  15. PRINT LNUM(22 / 7, 5)
  16. PRINT "6 decimal places  ";
  17. PRINT LNUM(22 / 7, 6)
  18. PRINT "7 decimal places  ";
  19. PRINT LNUM(22 / 7, 7)
  20. PRINT "8 decimal places  ";
  21. PRINT LNUM(22 / 7, 8)
  22. PRINT "9 decimal places  ";
  23. PRINT LNUM(22 / 7, 9)
  24. PRINT "10 decimal places ";
  25. PRINT LNUM(22 / 7, 10)
  26.  
  27.  
  28.  
  29.  
  30. FUNCTION LNUM# (num#, lenght)
  31.     '1.2345, 2  =>  1.23
  32.     integernum# = num# \ 1
  33.     decimalnum# = num# / 1 - num# \ 1
  34.     n = 10 ^ lenght
  35.     newdecimalnum# = decimalnum# * n
  36.     newdecimalnum# = INT(newdecimalnum#) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  37.     newdecimalnum# = newdecimalnum# / n
  38.     LNUM# = integernum# + newdecimalnum#
  39.  
  40.  

or FLOAT:

Code: QB64: [Select]
  1.  
  2. PRINT LNUM(0.0001, 4)
  3. PRINT 22 / 7
  4. PRINT LNUM(22 / 7, 1)
  5. PRINT LNUM(22 / 7, 5)
  6.  
  7. PRINT "1 decimal place   ";
  8. PRINT LNUM(22 / 7, 1)
  9. PRINT "2 decimal places  ";
  10. PRINT LNUM(22 / 7, 2)
  11. PRINT "3 decimal places  ";
  12. PRINT LNUM(22 / 7, 3)
  13. PRINT "4 decimal places  ";
  14. PRINT LNUM(22 / 7, 4)
  15. PRINT "5 decimal places  ";
  16. PRINT LNUM(22 / 7, 5)
  17. PRINT "6 decimal places  ";
  18. PRINT LNUM(22 / 7, 6)
  19. PRINT "7 decimal places  ";
  20. PRINT LNUM(22 / 7, 7)
  21. PRINT "8 decimal places  ";
  22. PRINT LNUM(22 / 7, 8)
  23. PRINT "9 decimal places  ";
  24. PRINT LNUM(22 / 7, 9)
  25. PRINT "10 decimal places ";
  26. PRINT LNUM(22 / 7, 10)
  27.  
  28.  
  29.  
  30.  
  31. FUNCTION LNUM## (num##, lenght)
  32.     '1.2345, 2  =>  1.23
  33.     integernum## = num## \ 1
  34.     decimalnum## = num## / 1 - num## \ 1
  35.     n = 10 ^ lenght
  36.     newdecimalnum## = decimalnum## * n
  37.     newdecimalnum## = INT(newdecimalnum##) 'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  38.     newdecimalnum## = newdecimalnum## / n
  39.     LNUM## = integernum## + newdecimalnum##
  40.  
  41.  

Petr, yours isn't rounding and more complicated than needs be, IMHO ;-))
« Last Edit: February 03, 2020, 12:07:59 pm by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 4 / 6 = .67
« Reply #23 on: February 03, 2020, 12:25:27 pm »
Too Complicated... It's difficult with you, guys, here. You still have some reservations :) Look, BPlus, it doesn't rounding because I didn't mean to. If you look at my source code, the row where this is:

  newdecimalnum ## = INT (newdecimalnum ##) 'or comment this row and rewrite it as newdecimalnum = newdecimalnum \ 1 for rounding outputs.

It gives an answer. When you rewrite this, you get a rounded output.
But what surprises me. Neither mine nor Steve's function returns more than a 15-digit decimal. Why? Although a larger decimal number does not seem to be meaningful, it still does the same outputs. That is, both DOUBLE and FLOAT return the same output?

Try this:

Code: [Select]
DIM A AS DOUBLE
DIM b AS _FLOAT

A# = 0.12345678901234567890
b## = 0.12345678901234567890

PRINT A#
PRINT b##

Why?

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: 4 / 6 = .67
« Reply #24 on: February 03, 2020, 12:52:55 pm »
That is, both DOUBLE and FLOAT return the same output?

Try this:

Code: [Select]
DIM A AS DOUBLE
DIM b AS _FLOAT

A# = 0.12345678901234567890
b## = 0.12345678901234567890

PRINT A#
PRINT b##

Why?
Float is broken, you may as well stick to double and forget about Float

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 4 / 6 = .67
« Reply #25 on: February 03, 2020, 01:11:36 pm »
Float is broken, you may as well stick to double and forget about Float

Oh, I did not know. Broke might imply it did work at one time (and could be fixed).

How broke? is it just is the same as DOUBLE? or worse?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: 4 / 6 = .67
« Reply #26 on: February 03, 2020, 01:24:30 pm »
Oh, I did not know. Broke might imply it did work at one time (and could be fixed).

How broke? is it just is the same as DOUBLE? or worse?

DOUBLE is stored as an 8 byte value in memory.
_FLOAT is stored as a 32 byte value — but only 10 bytes are actually used.  The other 22  bytes are basically reserved for future compatibility/use.

_FLOATS hold a little more precision than DOUBLE, but not much more (10 bytes verses 8), and they use 4x the memory.  Unless you absolutely need that extra precision, or unless you want to prepare for future implementation, in most cases you’re just as well off using DOUBLE as you are _FLOAT.

(One other cavet which I’ve noticed with _FLOAT: it often processes faster than double as many internal routines seem geared to work with float natively, and they don’t need to convert type values/offsets to process properly.  This may just be a result of the processes I use personally, so others may have different results, but it tends to hold true for me that float is faster than double/single.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 4 / 6 = .67
« Reply #27 on: February 03, 2020, 01:30:46 pm »
Too Complicated... It's difficult with you, guys, here. You still have some reservations :) Look, BPlus, it doesn't rounding because I didn't mean to. If you look at my source code, the row where this is:

  newdecimalnum ## = INT (newdecimalnum ##) 'or comment this row and rewrite it as newdecimalnum = newdecimalnum \ 1 for rounding outputs.

It gives an answer. When you rewrite this, you get a rounded output.
But what surprises me. Neither mine nor Steve's function returns more than a 15-digit decimal. Why? Although a larger decimal number does not seem to be meaningful, it still does the same outputs. That is, both DOUBLE and FLOAT return the same output?

Try this:

Code: [Select]
DIM A AS DOUBLE
DIM b AS _FLOAT

A# = 0.12345678901234567890
b## = 0.12345678901234567890

PRINT A#
PRINT b##

Why?

Hi Petr,

In paper today it said I could argue with my friends, I hope

rounding seems to me the only way to get 4/6 = .67

overly complicated because it takes you 6 lines to do what can be done in one.

I rest my case.

I will buy you a beer if we ever meet :) and we can laugh at this BS.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 4 / 6 = .67
« Reply #28 on: February 03, 2020, 01:45:21 pm »
Thank you, Jack and Steve for explanation and BPlus for you nice answer.

I apologize for my exaggerated reactions, perhaps i give some form of male menstruation or something like that... alone don´t know.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: 4 / 6 = .67
« Reply #29 on: February 03, 2020, 02:13:32 pm »
Its testosterone, I can attest(icle)
You're not done when it works, you're done when it's right.