⎕VFI Question on Negative Number

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
Post Reply
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

⎕VFI Question on Negative Number

Post by PGilbert »

If you do in Dyalog APL the following:

      ⎕VFI '¯2'
1 ¯2

⎕VFI '-2'
0 0

you get that '-2' is not a correct number. Why is that ?

Oddly in APL+Win
      ⎕VI '-2'
will give 1, but it is stated in the documentation that
      '-'
is treated as
      '¯'
and in APLX
      ⎕VI '-2'
will return 0, like in Dyalog. Now I know that if one is using ⎕VFI to validate the character input of a user it should do something like
      ((text='-')/text)←'¯'
before using ⎕VFI, but again what's wrong with '-2' ?

Thanks
DanB|Dyalog

Re: ⎕VFI Question on Negative Number

Post by DanB|Dyalog »

-2, just like *2 or £2, is not a number in APL.
That's the way Dyalog (and others) implemented []VFI (or []FI).
Since this has been going on for years they can't simply change the behaviour now without seeing people's code breaking.
But if enough demand is met they may implement other features.
How do you see it working? With an extension to the left argument to specify whether - is acceptable? Using the new [:] operator?
PhilGray
Posts: 50
Joined: Sat Mar 13, 2010 7:55 pm

Re: ⎕VFI Question on Negative Number

Post by PhilGray »

Postby PhilGray on Sun Oct 30, 2011 8:28 am

How do you see it working? With an extension to the left argument to specify whether - is acceptable? Using the new [:] operator?



May I suggest a Return-Code of length 3 ?

RC[ 1 2 ] remaining the same, and RC [ 3 ] representing the "type" of "translation" done if the element/cell to be translated was not "conventional" .

e.g.: RC [ 3 ]

0 ... conventional translation = no 'exception' case
1 ... '-' was replaced with '¯'
2 ... a currency symbol was found and ignored
3 ... ',' was interpreted as a "." decimal-marker ( European versus American )
4 ... more than one number was found in the cell, of which only the first was used
5 ... etc.
6 ... etc.

The list could be expanded as new cases arise, thus easy to maintain.

Also, any reference to ⎕VFI in existing code could easily be "Found & Replaced" with a 2↑⎕VFI , or a cover function ( e.g. ∆∆VFI ) that does the 2↑ internally.

Just my 2 Cents.

PhilGray
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: ⎕VFI Question on Negative Number

Post by PGilbert »

Thanks Daniel for the education, this is answering my question. Since ⎕VFI can be used to validate the input text of a user that will use '-' for a negative number, I would like to suggest that a warning to that effect be added to the online help of ⎕VFI to help the newbies like me coming from another APL to port their code.
DanB|Dyalog

Re: ⎕VFI Question on Negative Number

Post by DanB|Dyalog »

PhilGray wrote:May I suggest a Return-Code of length 3 ?


You can, of course, but any change which is not upward compatible is unlikely to be met with enthusiasm.
There is a lot of code out there like

Code: Select all

  (V N)<-⎕VFI S 

which would break if ⎕VFI were to return a 3rd element.

The new ⍠ (Variant) operator would be an ideal choice for this.
For example we could have

Code: Select all

  (V N)←⎕vfi ⍠ 'Neg' '-¯' ⊢ S

to specify - and ¯ as NEGative decorators.
DanB|Dyalog

Re: ⎕VFI Question on Negative Number

Post by DanB|Dyalog »

WRT translating strings, the new []R operator makes changing strings a trivial thing:
1 ... '-' was replaced with '¯'
2 ... a currency symbol was found and ignored
3 ... ',' was interpreted as a "." decimal-marker ( European versus American )


you can do

Code: Select all

      from←'-'  '[$£¤€¥]' ','
      to←'¯' '' '.'
      ]disp ⎕VFI from ⎕r to ⊢ '3.3  -321  $32,45  -654.32¥'
┌→──────┬──────────────────────┐
│1 1 1 1│3.3 ¯321 32.45 ¯654.32│
└~─────→┴~────────────────────→┘

and even more complex expressions like single spaced numbers (e.g. 12 345,67),
numbers surrounded by parentheses to denote negative numbers (e.g. (32.56) )
and multiple character currencies (e.g. 123.45DK):

Code: Select all

      from←'[-(]'  '([$£¤€¥]|DK|\))' ',' '(\d) (\d)'
      to←'¯' '' '.'  '\1\2'
      ]disp ⎕VFI from ⎕r to ⊢ 'DK123 456 789,50  -65 124.32¥  (12 345 678.90)€'
┌→────┬─────────────────────────────────┐
│1 1 1│123456789.5 ¯65124.32 ¯12345678.9│
└~───→┴~───────────────────────────────→┘

The possibilities are endless.
PhilGray
Posts: 50
Joined: Sat Mar 13, 2010 7:55 pm

Re: ⎕VFI Question on Negative Number

Post by PhilGray »

Thanks Dan.

Of course we can use APL to solve these exceptions as you have shown , but my thinking was simply that having the Interpreter handle these "exceptions" at a (low) machine.code level would be ..
a) much faster in execution
b) more comfortable

Cheers
DanB|Dyalog

Re: ⎕VFI Question on Negative Number

Post by DanB|Dyalog »

I understand Phil, what I was driving at was that it would be nice to have all these features in the language but
the problem is we don't have a concensus (yet) and it might take time to
a. get one
b. get Dyalog (or any other implementor) to put it in the language

So in the meantime we can experiment (APL is pretty good at that) and with the new []R operator it is even easier.
Cheers
User avatar
AndyS|Dyalog
Posts: 263
Joined: Tue May 12, 2009 6:06 pm

Re: ⎕VFI Question on Negative Number

Post by AndyS|Dyalog »

I've logged an issue to amend the ⎕VFI documentation to show that numbers such as "-2" are not valid.
Post Reply