tweak Str$ for single and double
#1
I think that this is worth looking into
Code: (Select All)
Dim As Single x
For x = 1 To .05 Step -.05
    Print x
Next x

output
Code: (Select All)
1
.95
.9
.85
.8
.7499999
.6999999
.6499999
.5999999
.5499999
.4999999
.4499999
.3999999
.3499998
.2999998
.2499998
.1999998
.1499998
9.999985E-02

if you change the format string in the function qbs *qbs_str(float value) in libqb.cpp from "% .6E" to "% .6G" you get

Code: (Select All)
1
0.95
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.0999998
similarly results for the function qbs *qbs_str(double value), changing the "E" to "G"

before change

Code: (Select All)
1
.95
.9
.85
.7999999999999998
.7499999999999998
.6999999999999997
.6499999999999997
.5999999999999996
.5499999999999996
.4999999999999996
.4499999999999996
.3999999999999996
.3499999999999996
.2999999999999997
.2499999999999997
.1999999999999997
.1499999999999997
.0999999999999997

after changing "E" to "G"
the change to G messes up if the exponent goes above 99 so more work is needed to make it work for the full range

Code: (Select All)
1
0.95
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
Reply
#2
Just two problems with this type of solution to floating point values:

Code: (Select All)
Dim As Single x
Dim As Integer y
For x = 1 To .05 Step -.05
    Print x
Next x


Sleep

Cls
y = 100
For x = 1 To .05 Step -.05
    Print x, y / 100,
    If x = y / 100 Then Print "EQUAL" Else Print "NOT EQUAL"
    y = y - 5
Next x


1) You're not actually changing the values.  Just how they're displayed -- in this case you can see they actual values themselves don't match up between X and Y after the sleep statement.

2) The formatting and display gets all wacky once dealing with different variables inside that same PRINT statement.  The output for the second set of code after the SLEEP looks nothing like the code from before the SLEEP.  Extra zeros get tacked onto everything, and the value for one somehow has a nice 0998(something) trailing after it for X.


[Image: image.png]



I wouldn't necessarily call the above an improvement.
Reply
#3
Note if you change that to F instead of E or G, you can at least get the following:


[Image: image.png]

At least the values PRINT the same, but as it's rather obvious to see -- they really aren't.  

Now, who's going to field the error reports and complaints when people start showing up on the forums and posting, "Somethings BROKE with your math!!  You can see from the PRINT statement, my two values are the same, and yet, they're reporting that they're not!!   RAAAHHRRRRR!!!!"
Reply
#4
Hey, now I can't get all the wrong answers on my QBasic 1999 calculator app. Where's the promise of compatibility! Big Grin

Kidding aside, why can't QB64 simply piggy back in on the C math.h library? I might be missing something but it seems to me the posts at Stack Overflow produce considerably better accuracy than what QB spits out.

Pete
Reply
#5
Steve and Pete, you are right.
in retrospect it was a stupid idea.
the thing is, some people want/expect a certain output without any effort on their part
I have suggested this before, instead of a badly supported _Float, replace it with a decimal type.
Pete, you can easily use C's sprintf to convert numbers to string but that is too much trouble for most people
Reply
#6
Hey, you got that decfloat boat to float, so don't 'sail' yourself short!

[Image: 283v.gif]

Pete
Reply
#7
thanks Pete, it still need more polish though
Reply
#8
I know some Polish girls I could send your way... oops, you meant the other spelling. Wait... opens letter... Another memo to report to H.R. !? You've got to be kidding me!

Hey, if you get a chance, have a gander at the pickle related to this post over here: https://staging.qb64phoenix.com/showthre...5#pid10895

No I probably didn't remember my old rounding function for small transactions in dollars and cents, but in any case, nothing seems to work out with Mark's counter loop.

Pete
Reply
#9
(12-01-2022, 12:17 PM)Jack Wrote: Pete, you can easily use C's sprintf to convert numbers to string but that is too much trouble for most people
It's because people want to get carried away about it. It's after using "PRINT USING" for long enough and being frustrated its output cannot easily be put on a string, and the programmer doesn't care about using commas, currency symbols, padding characters and other things.

I don't care much about fake decimal places, calculators could do it too. I've never seen it in action, only saw myself repeatedly pressing "square root" key until the calculator kept repeating an answer of "1." I used to have a calculator that displayed a row of "00000000" after an exponent went higher than 99. Also loved the calculator that dealt with division by zero with "0.E".

For a few months I had a calculator that I bought at Radio Shack which allowed simple BASIC programs. It sucked it had only one line of text for letters, numbers and symbols. I was dumb enough to bring it to school and resulted in its being lost being left in the bus. :/
Reply
#10
Steve left his calculator in the back seat of a school bus when he was a kid, too. Fortunately for him, he realized it at the door, and it was just a short walk to retrieve it! Big Grin

Hey I updated that ASCII Invaders game for you in the thread with a sub replacement so you can enter 2 initials, instead of 3.

Pete
Reply




Users browsing this thread: 3 Guest(s)