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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something for the weekend #6: VFP functions 2

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,516
Scotland
An easy one this time. I expect we'll see some good replies before the end of Friday.

Can you think of any built-in VFP function that is always guaranteed to return exactly the same result every time you use it?

Just to be clear, the function will either take no parameters; or, if it takes one or more parameters, it will return the same result regardless of what parameters you pass.

Off-hand, I can think of one possible contender, although I'm not completely sure of it.

Before you post a reply, please see my note below re spoilers.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
SYS(13) - Printer Status - "For versions of Visual FoxPro running on Windows, SYS(13) always returns READY." (Is it safe to assume that Visual Foxpro will always run under Windows?)

Well, NOW it's a safe assumption. But there was a Mac version of VFP3. Nobody took it seriously, and for good reason, but it did exist.
 
danfreeman said:
Well, NOW it's a safe assumption. But there was a Mac version of VFP3. Nobody took it seriously, and for good reason, but it did exist.

I just tested SYS(13) in Mac VFP3. It returns "READY" even though I have no printer connected to the computer.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Mike Lewis said:
- SYS(2006) - Current Graphics Card - "Returns the type of graphics card and monitor you are using." On my system, the returned value is "Color/Color". Can we assume that would always be the same? (Dan suggested something similar.)

In Mac VFP3, this returns "32-bit Color QuickDraw" on my machine which eliminates SYS(2006) from the competition.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
I just tested SYS(13) in Mac VFP3. It returns "READY" even though I have no printer connected to the computer.

That's correct and it's because VFP does not communicate directly with the printer (in either Windows or Mac). It communicates with the print spooler, which is always present whether there's a printer at the end of it or not.
 
Just to recap on some of the above points ...

There are clearly several functions that return information about the environment, and which always return the same value when running under Visual FoxPro for Windows. (And isn't it amazing that at least one person here was able to test these under VFP 3.0 for Mac?)

When I started this thread, the function I had in mind was PI(). At first, I was worried that this function is sensetive to SET DECIMALS. But that only affects how the returned value is displayed, not how it is stored internally. But I was confused by the following:

[tt]SET DECIMALS TO 2
x2 = PI() && X2 should be 3.141592653589793
? x2 && displays 3.14, as expected
SET DECIMALS TO 4
? x2 && still displays 3.14, not 3.1416, as expected
SET DECIMALS TO 18
? x2 && still displays 3.14[/tt]

So the precision of the value stored in the variable reflects the setting at the time the variable was created, not what's in force when it's displayed.

And yet:

[tt]? STR(x2, 16, 15) && displays 3.14159265358979[/tt]

You can see why I was confused.

Whatever .... PI() was the function I had in mind, as several of you immediately realised.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
>So the precision of the value stored in the variable reflects the setting at the time the variable was created, not what's in force when it's displayed.
That's not the whole truth, eg consider:

Code:
Set Decimals To 2
lnPi = Pi()*1.0000
?lnPi

Unlike any other language VFP considers a side calculation of the precisions of values involved. Unfortunately it's not always as nice, eg in division.

Bye, Olaf.
 
Only for fun.
I guess this are the rules :
1) Variables
a) Multiplication
Number of decimal = sum(number of decimals)
b) Division
Number of decimals = max(SET(Decimals) + 2, sum(number of decimals))

2) Constants
a) Double constants
Number of decimals = max(SET(Decimals) , sum(number of decimals))
b) Integer constants
If the result is an integer then Number of decimals = 0
Else Number of decimals = SET(Decimals)

This is my test
Code:
LOCAL setdec
setdec = SET("Decimals")
doit(0)
doit(1)
doit(2)
doit(3)
doit(4)
SET DECIMALS TO &setdec

PROCEDURE doit
LPARAMETERS setdec
LOCAL lnx
CLEAR
SET DECIMALS TO setdec
?"Set decimals to",SET("Decimals")
?
?"Multiplication"
lnx = PI()
? "lnx " , lnx
lnx = lnx * 1
? "lnx = lnx * 1" , lnx 
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx 
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx 
lnx = lnx * 1.00
? "lnx = lnx * 1.00" , lnx 
? "multiplication between two double constants","3.1*3.1=", 3.1 * 3.1
?
? "Division"
lnx = PI()
? "lnx " , lnx
lnx = lnx / 1
? "lnx = lnx / 1" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.00
? "lnx = lnx / 1.00" , lnx 
lnx = lnx / 1.000
? "lnx = lnx / 1.000" , lnx 
lnx = lnx / 1.0000
? "lnx = lnx / 1.0000" , lnx 
lnx = lnx / 1.00000
? "lnx = lnx / 1.00000" , lnx 
lnx = lnx / 1.000000
? "lnx = lnx / 1.000000" , lnx 
? "division between two double constants","3.1/3.1=", 3.1 / 3.1

?
?"Multiplication"
lnx = 1
? "lnx " , lnx
lnx = lnx * 1
? "lnx = lnx * 1" , lnx 
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx 
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx 
lnx = lnx * 1.00
? "lnx = lnx * 1.00" , lnx 
? "multiplication between two integer constants","2*1=", 2*1,"1*2=", 1*2
?
? "Division"
lnx = 1
? "lnx " , lnx
lnx = lnx / 1
? "lnx = lnx / 1" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.00
? "lnx = lnx / 1.00" , lnx 
lnx = lnx / 1.000
? "lnx = lnx / 1.000" , lnx 
lnx = lnx / 1.0000
? "lnx = lnx / 1.0000" , lnx 
lnx = lnx / 1.00000
? "lnx = lnx / 1.00000" , lnx 
lnx = lnx / 1.000000
? "lnx = lnx / 1.000000" , lnx 
? "division between two integer constants","2/1=", 2/1,"1/2=", 1/2

WAIT
ENDPROC

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
We can't be too thankful or proud of this feature, though: Consider this bad example about the ^ operator:
Code:
? .123*.123
? .123^2
For highest precision computing you should SET DECIMALS TO 18 (the output of .123^2 then also doesn't differ from .123*.123 in the numeric output, just in the indentation.

I did set decimals to 18 as default for a module computing percentages of ingredients of formulas/recipes, which even may need recursion.
You would still need to consider small corrections in cases of not having a 100% sum and overall it wasn't simple to come up with something acceptable.

Bye, Olaf.
 
I guess the rule for power is
Number of decimals (b ^ e) = max(SET(Decimals) , number of decimals(b))
Code:
LOCAL setdec
setdec = SET("Decimals")
doit(0)
doit(1)
doit(2)
doit(3)
doit(4)
SET DECIMALS TO &setdec

PROCEDURE doit
LPARAMETERS setdec
LOCAL lnx
CLEAR
SET DECIMALS TO setdec
?"Set decimals to",SET("Decimals")
?
?"Multiplication"
lnx = PI()
? "lnx = " , lnx,"lnx ^ 2 = " , lnx ^ 2,"lnx ^ 2.231 = " , lnx ^ 2.231
lnx = lnx * 1
? "lnx = lnx * 1" , lnx ,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx ,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx ,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1.00
? "lnx = lnx * 1.00" , lnx ,"lnx ^ 2 = " , lnx ^ 2
? "multiplication between two double constants","3.1*3.1=", 3.1 * 3.1,"3.1 ^ 2 =",3.1 ^ 2
?
? "Division"
lnx = PI()
? "lnx = " , lnx
lnx = lnx / 1
? "lnx = lnx / 1" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.00
? "lnx = lnx / 1.00" , lnx 
lnx = lnx / 1.000
? "lnx = lnx / 1.000" , lnx 
lnx = lnx / 1.0000
? "lnx = lnx / 1.0000" , lnx 
lnx = lnx / 1.00000
? "lnx = lnx / 1.00000" , lnx 
lnx = lnx / 1.000000
? "lnx = lnx / 1.000000" , lnx 
? "division between two double constants","3.1/3.1=", 3.1 / 3.1

?
?"Multiplication"
lnx = 1
? "lnx = " , lnx,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1
? "lnx = lnx * 1" , lnx ,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx ,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1.0
? "lnx = lnx * 1.0" , lnx ,"lnx ^ 2 = " , lnx ^ 2
lnx = lnx * 1.00
? "lnx = lnx * 1.00" , lnx ,"lnx ^ 2 = " , lnx ^ 2
? "multiplication between two integer constants","2*1=", 2*1,"1*2=", 1*2,"1 ^ 2 =",1 ^ 2
?
? "Division"
lnx = 1
? "lnx = " , lnx
lnx = lnx / 1
? "lnx = lnx / 1" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.0
? "lnx = lnx / 1.0" , lnx 
lnx = lnx / 1.00
? "lnx = lnx / 1.00" , lnx 
lnx = lnx / 1.000
? "lnx = lnx / 1.000" , lnx 
lnx = lnx / 1.0000
? "lnx = lnx / 1.0000" , lnx 
lnx = lnx / 1.00000
? "lnx = lnx / 1.00000" , lnx 
lnx = lnx / 1.000000
? "lnx = lnx / 1.000000" , lnx 
? "division between two integer constants","2/1=", 2/1,"1/2=", 1/2

WAIT
ENDPROC

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
I'll just wind down this thread. Thanks to everyone who took part. The original question was an easy one, but it led to a lot of interesting discussion, which is always good.

I'm running out of ideas for brain-teasers, so next time we have one of these threads, perhaps someone else can come up with an interesting question.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I have something, that might be a good question, I'll need to investigate a bit to see, whether the answer still is valid.

Bye, Olaf.
 
I'll post it later - evening in CET, perhaps tomorrow morning. It'll have a little code with it, which I need to prepare.

Bye, Olaf.
 
This is only a little joke.
Create suspense, as in a thriller [smile]

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Of course this will have a new thread.

As a teaser: It's about Error 111.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top