Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to avoid wrong input 3

Status
Not open for further replies.

german12

Programmer
Nov 12, 2001
563
DE
I did not find the correct translation for the german mathematical expression "Fakultät"
which means
mathematics
Product, the factors of which are formed by the structure of the natural number series, starting with 1 (symbol:!; E.g. 1 · 2 · 3 · 4 · 5 = 5!)

Is it faculty? Websters Encylopidea does not show it as mathematical...
What ever....


I tried it with a simple code (see below) to calculate it - but now there are other questions of principle for me:

a) How can you best ensure that
only whole numbers >0 (no text, no zeros, no broken numbers etc) are allowed for input
(only 1, 2, 3 ff.are allowed) but only up to the limit,
which VFP for the maximum possible visual display of large numbers?

(for example eg. when input for m.mynumber = 1000 the result is toö big - and VFP shows this.

Screenshot_2021-10-05_234353_omuy4n.png


b) Can I prevent an input which leads to such a "******"-display by code when I do not try it before what the max. Input must be?

Thanks
Klaus



Code:
*Faculty.prgThis program calculates faculties of a given number
*In this example the result of 5! will be calculated

CLEA
m.mynumber = 5   &&this number is the basis for 5!
m.z = 1          

FOR x = m.mynumber TO 1 STEP -1
	m.z=x*m.z
ENDFOR

? "The faculty of ",ALLTRIM(STR(m.mynumber))+"!", " = ",m.z

Peace worldwide - it starts here...
 
It's called factorial.

In VFP every variable stores double values (floating point double precision). That stores integers precisely up to the point where the exponent of the floating point number can't be 1 anymore. That doesn't mean no more integers can be stored, but then the precision is 2. Sounds funny, but it means you'll only be able to increase by 2 and have to skip odd numbers. There comes a pioint the steps have to be 4, 8, 16, and so on. The range ends with the double precision floating point range and you find that in the help in "System capacities".

It's easy to find out from when VFP has to change to scientific notation (with E+nn) and from when it completely fails to convert with STR():

Code:
factor = 0
factorial = 1

DO WHILE NOT "E" $ STR(m.factorial,20)
   ? m.factor, m.factorial
   factor = m.factor + 1
   factorial = m.factorial * m.factor
ENDDO

? 'end of precise results'

DO WHILE NOT "*" $ STR(m.factorial,80)
   ? m.factor, m.factorial
   factor = m.factor + 1
   factorial = m.factorial * m.factor
ENDDO

? m.factor, m.factorial
? 'This is the end, my friend'

I get up to 20! with precise results and 170! with scientific notation, 171! is beyond VFPs double precision range.

If you want to know higher factorial numbers Python can go on until you exceed RAM.

Chriss
 
For question a) this means only allow inputs from 0 to 171 or even just up to 20.

Bind the textbox to an integer and you can only input an integer. Use a spinner control and you can set min/max values.

Chriss
 
Thanks Chriss - again I learned very much by your answer.
Klaus


Peace worldwide - it starts here...
 
Funny you should ask about factorials. That's one of the examples I'll be using to introduce recursion in my session at Visual Fox Fest later this month. (Ultimately, I'll be saying that you shouldn't use recursion for factorials, but they're still a good example to show the ideas.)

Tamar
 
It's the academic standard for introducing recursion. Will you cover tail recursion and turning recursion to iteration?
It's also nice to show how private variables can speed up recursion by not needing to pass in parameters and much more. I think this can be a very entertaining and educating session at the same time.

Chriss
 
My session includes cases where recursion comes early in the process and those where recursion comes late. I'm not looking at how to convert to iteration, though I mention that anything recursive can be done iteratively.

As for variables, I'm harping more on the need to declare them so you don't break things than on using privates, though my paper includes an example where a private variable lets you instrument a routine.

As for why factorials, it's because it's a simple example most people already know and that doesn't have a lot of overhead. I'm also looking at directory structures, but later after introducing the basics.

Tamar
 
I know your books explain it all, not just PUBLIC, LOCAL and how PRIVATE differs from them, how PARAMETERS differs from LPARAMETERS and what SET UDFPARAMETERS means and even what REGIONAL is.
I support what you say in
It's just still there in VFP9 or this wouldn't work correctly:
Code:
Clear
For lnI = 1 To 2
   Subroutine()
Endfor

Procedure Subroutine()
   Regional lnI

   For lnI = 1 To 3
      ? lnI
   ENDFOR
   
   DISPLAY MEMORY LIKE ln*
Endproc

Try without Regional and the subroutine is only called once.

Tamar Granor said:
my paper includes an example where a private variable lets you instrument a routine
Great to hear. Overall that you're still contributing to the forum.

I learned from someone saying there is a use of everything VFP provides. I have no use for Regional, but I think #DEFINE LOCAL Store .F. TO and starting every routine with PRIVATE ALL is a nice hack, too.

Chriss
 
Well, where I come from learning binomial coefficients - which teach factorial as a sidenote - are in the curriculum of every school, embedded within statistical mathematics.

Even if someone never learned about it, it's easily explained.

There are more practical examples, sure, you named one, but as Tamar said she's addressing directory structures later in the session.

Chriss
 
More about integer-sequences can be found here:
Link


Klaus


Peace worldwide - it starts here...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top