Rounding numbers

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Rounding numbers

Post by srvaldez »

about 5 years ago I made a bug report about the format function returning a zero with an exponent, sometime after, one of the FB developers contacted me about it, he wanted me to confirm that a certain format was correct but I am not a format expert, so I wrote a small VB6 program that would take a format string and when you clicked on the format button it would do the formatting, just in case someone wants to use it as a reference you can get the VB Format helper here

here's what it looks like
Image
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Rounding numbers

Post by coderJeff »

srvaldez wrote: Mar 25, 2024 19:46 @coderJeff
I tried hhr suggestion and it seems that he is right, however, I don't know if there are cases where the suggested change would cause a problem.
the format function is a complex beast and it seems that it's not finished, I know that it fails the tests with gcc-13.2 and up
See the note in hRound():

Code: Select all

		/* remove the fraction of the fraction to be compatible with
		   VBDOS (ie: 2.55 -> 2.5, not 2.6 as in VB6) */
Is the logic on line str_format.c:335 related to that?

Take care when comparing PRINT USING and FORMAT results, I think they do not share any code.

Also, in the fbc test-suite, I have found different results depending on target or different versions of toolchains and gcc or between 32-bit and 64-bit, so sometimes the format/print using tests in the test-suite are either disabled or limits are relaxed for match most precise digits.

Identifying the logic and testing near the edges of logic conditions (a daunting task test all logic) will be more productive than testing randomly.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Rounding numbers

Post by srvaldez »

hello coderJeff :)
"Is the logic on line str_format.c:335 related to that?"
I don't know, to me the format function is a mystery and counter intuitive, a function to be avoided
I didn't know that the FB format function was modeled after vbdos, I just tried to rewrite the format helper in vbdos but I was unsuccessful.
<edit>
I just looked up the format specifiers in VBDOS and it only accepts the following
Numeric expressions
# Digit position
- Placed after digit, prints trailing sign for negative numbers
. Decimal point position
, placed left of the decimal point, prints a comma every third digit
$$ Prints leading $
+ Position of number sign
** Fills leading spaces with *
^^^^ Prints exponential format
**$ Combines ** and $$

String expressions
& prints entire string
\ \ Prints first n characters, where n is the number of blanks between slashes + 2
! Prints only the first character of the string

don't know how dates and times are handled in Format$
Last edited by srvaldez on Mar 29, 2024 22:50, edited 2 times in total.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Rounding numbers

Post by coderJeff »

srvaldez wrote: Mar 29, 2024 20:58 I didn't know that the FB format function was modeled after vbdos
Until I seen that comment in the source, I never would have guessed. Proving out FORMAT and PRINT USING is more than I would want to take on right now, otherwise I personally have no objection to changes if the output is more favourable for users.
Post Reply