Would a sscanf format %q for turning numbers (possibly with decimals) to Gmp.mpq objects be a good, bad, easy and/or hard addition to make? (I was just considering looking at it when I wake up later on today.)
When do you need it?
/ Mirar
Previous text:
2004-02-07 05:31: Subject: sscanf and Gmp.mpq
Would a sscanf format %q for turning numbers (possibly with decimals) to Gmp.mpq objects be a good, bad, easy and/or hard addition to make? (I was just considering looking at it when I wake up later on today.)
/ Johan Sundström (Achtung Liebe!)
Not sure whether I understand the question, but whenever you deal with rational numbers and do not want to lose precision in calculations you want to use Gmp.mpq, rather than floats, doubles or Gmp.mpf:s.
Or, if you were referring to my particular current application, I am toying with an SVG optimiser/pretty-printer. (It's in good style not to degrade the quality of the input file.)
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 08:07: Subject: sscanf and Gmp.mpq
When do you need it?
/ Mirar
But you said "numbers (possibly with decimals)", which suggest that you are not, in fact, dealing with rational numbers.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 15:15: Subject: sscanf and Gmp.mpq
Not sure whether I understand the question, but whenever you deal with rational numbers and do not want to lose precision in calculations you want to use Gmp.mpq, rather than floats, doubles or Gmp.mpf:s.
Or, if you were referring to my particular current application, I am toying with an SVG optimiser/pretty-printer. (It's in good style not to degrade the quality of the input file.)
/ Johan Sundström (Achtung Liebe!)
Then I was overly vague there; for this application, 10.2 is the very much exact rational number 102/10.
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:18: Subject: sscanf and Gmp.mpq
But you said "numbers (possibly with decimals)", which suggest that you are not, in fact, dealing with rational numbers.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
That is not the standard interpretation though, and so building such assumptions into sscanf seems plain wrong.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 15:23: Subject: sscanf and Gmp.mpq
Then I was overly vague there; for this application, 10.2 is the very much exact rational number 102/10.
/ Johan Sundström (Achtung Liebe!)
4.711 can easily be converted to the fraction 4711/1000.
But I'm also not sure I see the point. If Johan justs want to output the same numbers without any changes, why not just keep them around as strings?
/ Niels Möller (vässar rödpennan)
Previous text:
2004-02-07 15:18: Subject: sscanf and Gmp.mpq
But you said "numbers (possibly with decimals)", which suggest that you are not, in fact, dealing with rational numbers.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
I'm going to do simplification on input data such as finding uselessly verbose ways of drawing a straight line between A and B; for instance, via a point C between the two. Otherwise, strings would of course be quite satisfactory.
And while floating point precision would probably be just fine for the majority of users, better support for the Gmp primitives in Pike wouldn't hurt. AFAIK only Gmp.mpz is exercised regularly and to any greater extent in Pike today, making it less than convenitnt to use the others. Call it more of a reason to make the work more compelling.
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:25: Subject: sscanf and Gmp.mpq
4.711 can easily be converted to the fraction 4711/1000.
But I'm also not sure I see the point. If Johan justs want to output the same numbers without any changes, why not just keep them around as strings?
/ Niels Möller (vässar rödpennan)
Makes some sense, althugh I'm not convinced that a %q specifier in sscanf is the way to go. It's not obvious exactly what it should match. Some formats that it might support:
17 5/7 -5/7 5/-7 4.711 4711e-3 3.14e-4711 5e3/7 5/7e3
/ Niels Möller (vässar rödpennan)
Previous text:
2004-02-07 15:31: Subject: sscanf and Gmp.mpq
I'm going to do simplification on input data such as finding uselessly verbose ways of drawing a straight line between A and B; for instance, via a point C between the two. Otherwise, strings would of course be quite satisfactory.
And while floating point precision would probably be just fine for the majority of users, better support for the Gmp primitives in Pike wouldn't hurt. AFAIK only Gmp.mpz is exercised regularly and to any greater extent in Pike today, making it less than convenitnt to use the others. Call it more of a reason to make the work more compelling.
/ Johan Sundström (Achtung Liebe!)
0017 17e-420000000000000
Speciellt den senare kanske blir intressant.
/ Mirar
Previous text:
2004-02-07 15:36: Subject: sscanf and Gmp.mpq
Makes some sense, althugh I'm not convinced that a %q specifier in sscanf is the way to go. It's not obvious exactly what it should match. Some formats that it might support:
17 5/7 -5/7 5/-7 4.711 4711e-3 3.14e-4711 5e3/7 5/7e3
/ Niels Möller (vässar rödpennan)
Hm. What obstacles might be involved in the second? (I did not assume I could stand on floats as an intermediary form in parsing here; was that your line of thought?)
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:40: Subject: sscanf and Gmp.mpq
0017 17e-420000000000000
Speciellt den senare kanske blir intressant.
/ Mirar
A thought whether 0017 means 17, or 15. But no more. :)
/ Mirar
Previous text:
2004-02-07 15:44: Subject: sscanf and Gmp.mpq
Hm. What obstacles might be involved in the second? (I did not assume I could stand on floats as an intermediary form in parsing here; was that your line of thought?)
/ Johan Sundström (Achtung Liebe!)
If you try representing it as an mpq, you'll run out of memory.
/ Niels Möller (vässar rödpennan)
Previous text:
2004-02-07 15:44: Subject: sscanf and Gmp.mpq
Hm. What obstacles might be involved in the second? (I did not assume I could stand on floats as an intermediary form in parsing here; was that your line of thought?)
/ Johan Sundström (Achtung Liebe!)
Oh, the second.
17e-4200000000000000 is represented in Gmp.mpq as something with many many decimals. It will allocate a lot of memory.
/ Mirar
Previous text:
2004-02-07 15:44: Subject: sscanf and Gmp.mpq
Hm. What obstacles might be involved in the second? (I did not assume I could stand on floats as an intermediary form in parsing here; was that your line of thought?)
/ Johan Sundström (Achtung Liebe!)
That's about those I was thinking of, yes. Assuming a sscanf modifier is /not/ the way to go, what /would/ be?
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:36: Subject: sscanf and Gmp.mpq
Makes some sense, althugh I'm not convinced that a %q specifier in sscanf is the way to go. It's not obvious exactly what it should match. Some formats that it might support:
17 5/7 -5/7 5/-7 4.711 4711e-3 3.14e-4711 5e3/7 5/7e3
/ Niels Möller (vässar rödpennan)
A Gmp.mpq constructor that takes all of the above formatted strings, I think would be a start. (And Gmp.mpf...)
/ Mirar
Previous text:
2004-02-07 15:40: Subject: sscanf and Gmp.mpq
That's about those I was thinking of, yes. Assuming a sscanf modifier is /not/ the way to go, what /would/ be?
/ Johan Sundström (Achtung Liebe!)
Extract the string you want, and pass it on to the mpq constructor?
I don't know what formats Gmp.mpq(string) supports.
/ Niels Möller (vässar rödpennan)
Previous text:
2004-02-07 15:40: Subject: sscanf and Gmp.mpq
That's about those I was thinking of, yes. Assuming a sscanf modifier is /not/ the way to go, what /would/ be?
/ Johan Sundström (Achtung Liebe!)
Hm. Sounds like that might meet the Comstedt seal of approval. And whichever way I go, it would make sense to have that competence in the constructor anyway.
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:46: Subject: sscanf and Gmp.mpq
Extract the string you want, and pass it on to the mpq constructor?
I don't know what formats Gmp.mpq(string) supports.
/ Niels Möller (vässar rödpennan)
It could be an interesting addition to sscanf to have it create an object when it matches a string.
Gmp.mpq myvar; sscanf("17","%q",Gmp.mpq,myvar); // == 1
/ Mirar
Previous text:
2004-02-07 15:55: Subject: sscanf and Gmp.mpq
Hm. Sounds like that might meet the Comstedt seal of approval. And whichever way I go, it would make sense to have that competence in the constructor anyway.
/ Johan Sundström (Achtung Liebe!)
Um, first of all, shouldn't the conversion character for that be %O? Secondly, how would sscanf know how many characters of the string to consume?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 16:11: Subject: sscanf and Gmp.mpq
It could be an interesting addition to sscanf to have it create an object when it matches a string.
Gmp.mpq myvar; sscanf("17","%q",Gmp.mpq,myvar); // == 1
/ Mirar
Um, first of all, shouldn't the conversion character for that be %O?
Um, no? I'd consider a sscanf( "17", "%O", myvar ); that sets myvar to anything but the integer (int type) 17 buggy.
Secondly, how would sscanf know how many characters of the string to consume?
Implementation defined; either, one settles for Niels' suggestion, or something more to your liking that does not eat ".":s, "/":s and so on (if my general understanding of your likings is even half correct :-).
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 16:13: Subject: sscanf and Gmp.mpq
Um, first of all, shouldn't the conversion character for that be %O? Secondly, how would sscanf know how many characters of the string to consume?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Implementation defined;
Um, there is only one implementation of Pike, the question is how this implementation is proposed to define it. I have not seen any suggesion from Nisse in this matter.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 16:18: Subject: sscanf and Gmp.mpq
Um, first of all, shouldn't the conversion character for that be %O?
Um, no? I'd consider a sscanf( "17", "%O", myvar ); that sets myvar to anything but the integer (int type) 17 buggy.
Secondly, how would sscanf know how many characters of the string to consume?
Implementation defined; either, one settles for Niels' suggestion, or something more to your liking that does not eat ".":s, "/":s and so on (if my general understanding of your likings is even half correct :-).
/ Johan Sundström (Achtung Liebe!)
Something which reads 17, 5/7, -5/7, 5/-7, 4.711, 4711e-3, 3.14e-4711, 5e3/7 and 5/7e3, but for instance does not read "kladdkaka". You usually are good at finding borderline cases that could be troublesome -- have you any examples in mind?
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 16:28: Subject: sscanf and Gmp.mpq
Implementation defined;
Um, there is only one implementation of Pike, the question is how this implementation is proposed to define it. I have not seen any suggesion from Nisse in this matter.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Why should it not read "kladdakaka"? If the program passed is for example Stdio.File, then "kladdkaka" might be a perfectly acceptable choice. I don't see why your examples should be more suited to pass to constructors in general (as opposed to your specific example) than other strings.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 16:37: Subject: sscanf and Gmp.mpq
Something which reads 17, 5/7, -5/7, 5/-7, 4.711, 4711e-3, 3.14e-4711, 5e3/7 and 5/7e3, but for instance does not read "kladdkaka". You usually are good at finding borderline cases that could be troublesome -- have you any examples in mind?
/ Johan Sundström (Achtung Liebe!)
You lost me. I am still talking of the specific case of a format flag that knows we are sending stuff to a Gmp.mpq constructor, and not any constructor in general. I find it reasonable to pass on some knowledge of what that constructor handles to sscanf if implemented this way.
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 16:39: Subject: sscanf and Gmp.mpq
Why should it not read "kladdakaka"? If the program passed is for example Stdio.File, then "kladdkaka" might be a perfectly acceptable choice. I don't see why your examples should be more suited to pass to constructors in general (as opposed to your specific example) than other strings.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Then you are commenting in the wrong thread. I was commenting on Mirar's generic object construction converter.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 16:49: Subject: sscanf and Gmp.mpq
You lost me. I am still talking of the specific case of a format flag that knows we are sending stuff to a Gmp.mpq constructor, and not any constructor in general. I find it reasonable to pass on some knowledge of what that constructor handles to sscanf if implemented this way.
/ Johan Sundström (Achtung Liebe!)
Ah, I didn't catch on to the change of scope.
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 16:54: Subject: sscanf and Gmp.mpq
Then you are commenting in the wrong thread. I was commenting on Mirar's generic object construction converter.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Well, there are some small things to iron out. :)
Maybe sscanf could ask the object how many characters it wants.
But %O already has a definition, ie the reverse of sprintf %O for at least simple datatypes (not object, program).
/ Mirar
Previous text:
2004-02-07 16:13: Subject: sscanf and Gmp.mpq
Um, first of all, shouldn't the conversion character for that be %O? Secondly, how would sscanf know how many characters of the string to consume?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Since the functionality is the opposite of sprintf("%O"), I think it's reasonable to use %O. The trick is of course that you'd need to provide a program sometimes but not always.
Maybe sscanf could ask the object how many characters it wants.
Slight causality problem here: Since the string is to be sent to the constructor, the length must be known before there even exists an object.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 16:52: Subject: sscanf and Gmp.mpq
Well, there are some small things to iron out. :)
Maybe sscanf could ask the object how many characters it wants.
But %O already has a definition, ie the reverse of sprintf %O for at least simple datatypes (not object, program).
/ Mirar
Would it hurt much to feed the constructor the remainder of the string and have the genereated object remember how much of it that it wanted?
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 16:57: Subject: sscanf and Gmp.mpq
Since the functionality is the opposite of sprintf("%O"), I think it's reasonable to use %O. The trick is of course that you'd need to provide a program sometimes but not always.
Maybe sscanf could ask the object how many characters it wants.
Slight causality problem here: Since the string is to be sent to the constructor, the length must be known before there even exists an object.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
I'm definitely seeing ghosts now; I almost detected a faint hint of an implied practical perspective somewhere there. ;-)
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 17:04: Subject: sscanf and Gmp.mpq
From an aesthetical point of view: Yes. :-)
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
The functionality is not the opposite of sprintf("%O"):
| > sprintf("%O",Gmp.mpq(1234,4578)); | (1) Result: "Gmp.mpq(617/2289)" | > sprintf("%O",17); | (2) Result: "17" | > array_sscanf("17","%O"); | (3) Result: ({ /* 1 element */ | 17 | })
Theoretically: | > array_sscanf("Gmp.mpq(617/2289)","%O"); | (x) Result: ({ /* 1 element */ | Gmp.mpq(617/2289) | })
but here we want
| > array_sscanf("617/2289","%q"); | (x) Result: ({ /* 1 element */ | Gmp.mpq(617/2289) | })
Slight causality problem here: Since the string is to be sent to the constructor, the length must be known before there even exists an object.
Yes... It could send the rest of the string, and get the remainings as a result (via another function, if create never can put anything on the stack).
But another idea would be to use a flag (*-like) on all string reading types to sscanf, so you could use %[].
/ Mirar
Previous text:
2004-02-07 16:57: Subject: sscanf and Gmp.mpq
Since the functionality is the opposite of sprintf("%O"), I think it's reasonable to use %O. The trick is of course that you'd need to provide a program sometimes but not always.
Maybe sscanf could ask the object how many characters it wants.
Slight causality problem here: Since the string is to be sent to the constructor, the length must be known before there even exists an object.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
The functionality is not the opposite of sprintf("%O"):
On a higher level of abstraction it is. sprintf("%O") converts an object to a (class specfic) string representation. What your proposed conversion character does is to convert a (class specific) string representation to an object.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 17:19: Subject: sscanf and Gmp.mpq
The functionality is not the opposite of sprintf("%O"):
| > sprintf("%O",Gmp.mpq(1234,4578)); | (1) Result: "Gmp.mpq(617/2289)" | > sprintf("%O",17); | (2) Result: "17" | > array_sscanf("17","%O"); | (3) Result: ({ /* 1 element */ | 17 | })
Theoretically: | > array_sscanf("Gmp.mpq(617/2289)","%O"); | (x) Result: ({ /* 1 element */ | Gmp.mpq(617/2289) | })
but here we want
| > array_sscanf("617/2289","%q"); | (x) Result: ({ /* 1 element */ | Gmp.mpq(617/2289) | })
Slight causality problem here: Since the string is to be sent to the constructor, the length must be known before there even exists an object.
Yes... It could send the rest of the string, and get the remainings as a result (via another function, if create never can put anything on the stack).
But another idea would be to use a flag (*-like) on all string reading types to sscanf, so you could use %[].
/ Mirar
On a higher level of abstraction it is. sprintf("%O") converts an object to a (class specfic) string representation.
Yes, and %O is a theoretically class specific representation back.
But we're talking here of the reversal of sprintf("%f",object) rather then sprintf("%O",object).
/ Mirar
Previous text:
2004-02-07 17:42: Subject: sscanf and Gmp.mpq
The functionality is not the opposite of sprintf("%O"):
On a higher level of abstraction it is. sprintf("%O") converts an object to a (class specfic) string representation. What your proposed conversion character does is to convert a (class specific) string representation to an object.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
So maybe what we should really be using then is sscanf("%f"). All we need is the ability to specify an object whose _sscanf() to call for all conversion specifiers (or those marked with a special flag). The _sscanf can see that the expected format is 'f' or 'O' or whatever.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 18:24: Subject: sscanf and Gmp.mpq
On a higher level of abstraction it is. sprintf("%O") converts an object to a (class specfic) string representation.
Yes, and %O is a theoretically class specific representation back.
But we're talking here of the reversal of sprintf("%f",object) rather then sprintf("%O",object).
/ Mirar
This is smelling better by the minute.
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 18:28: Subject: sscanf and Gmp.mpq
So maybe what we should really be using then is sscanf("%f"). All we need is the ability to specify an object whose _sscanf() to call for all conversion specifiers (or those marked with a special flag). The _sscanf can see that the expected format is 'f' or 'O' or whatever.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Now we're getting somewhere. :)
¤ works for me, but I'm not sure if it works for everyone.
Is there any such thing as Gmp.mpq() without constructor argument?
/ Mirar
Previous text:
2004-02-07 18:28: Subject: sscanf and Gmp.mpq
So maybe what we should really be using then is sscanf("%f"). All we need is the ability to specify an object whose _sscanf() to call for all conversion specifiers (or those marked with a special flag). The _sscanf can see that the expected format is 'f' or 'O' or whatever.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
I used ¤ as a metasyntactic variable, since I didn't know off the top of my head which characters where not already taken (ether by an actual sscanf function, or a sprintf one which might make sense for sscanf as well).
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-07 19:43: Subject: sscanf and Gmp.mpq
Now we're getting somewhere. :)
¤ works for me, but I'm not sure if it works for everyone.
Is there any such thing as Gmp.mpq() without constructor argument?
/ Mirar
I meant, where is it useful enough not to send the data through a string to make Gmp.mpq objects out of after the sscanf?
If you add Gmp.mpq, also add Gmp.mpf.
The big benefit would of course be a review of Gmp.mpq's _sprintf. :)
/ Mirar
Previous text:
2004-02-07 15:15: Subject: sscanf and Gmp.mpq
Not sure whether I understand the question, but whenever you deal with rational numbers and do not want to lose precision in calculations you want to use Gmp.mpq, rather than floats, doubles or Gmp.mpf:s.
Or, if you were referring to my particular current application, I am toying with an SVG optimiser/pretty-printer. (It's in good style not to degrade the quality of the input file.)
/ Johan Sundström (Achtung Liebe!)
Gmp.mpq's _sprintf seems in much better shape than Gmp.mpf's, but both could probably use a second eye. :-)
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:27: Subject: sscanf and Gmp.mpq
I meant, where is it useful enough not to send the data through a string to make Gmp.mpq objects out of after the sscanf?
If you add Gmp.mpq, also add Gmp.mpf.
The big benefit would of course be a review of Gmp.mpq's _sprintf. :)
/ Mirar
If both mpq and mpf are added, there is the slight hassle of coming up with a good format character for mpf by the way. :-)
/ Johan Sundström (Achtung Liebe!)
Previous text:
2004-02-07 15:27: Subject: sscanf and Gmp.mpq
I meant, where is it useful enough not to send the data through a string to make Gmp.mpq objects out of after the sscanf?
If you add Gmp.mpq, also add Gmp.mpf.
The big benefit would of course be a review of Gmp.mpq's _sprintf. :)
/ Mirar
pike-devel@lists.lysator.liu.se