 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Little-Acorn
Joined: 06 Jul 2008 Posts: 111 Location: San Diego
|
Posted: Thu Sep 02, 2010 5:38 pm Post subject: Converting REAL number to character string in FTN95? |
|
|
I've used CNUM several times to convert an integer value such as 517, to a character string containing a 5, a 1, and a 7.
Is there a similar function that can convert a real number such as 3.14 to a character string containing a 3, a period, a 1, and a 4?
And, are there functions that can go the other way? Converting a character string to an integer, or to a real number?
Thank you! |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Thu Sep 02, 2010 6:24 pm Post subject: |
|
|
Look at "internal writes" (and their counterpart internal reads). They should do all that you need to convert either way between numbers and strings |
|
Back to top |
|
 |
Little-Acorn
Joined: 06 Jul 2008 Posts: 111 Location: San Diego
|
Posted: Thu Sep 02, 2010 9:08 pm Post subject: Re: |
|
|
brucebowler wrote: | Look at "internal writes" (and their counterpart internal reads). They should do all that you need to convert either way between numbers and strings |
Found them, thank you Bruce!
Seems kind of overhead-intensive. Is it? Or is that just my own mental hangover from the days when, when you wrote something to a file, you were actually writing to a physical 9-track tape?
So far I have gotten this done by taking the INT part and the FRACTIONal part, multiplying the fractional part by 1,000, INTing it, using CNUM on each, deleting extraneous spaces, and concatenating them into a single string with a period between them. (Speaking of overhead-intensive)
But hey, it works. Eventually.
In a perfect world, it would be nice if there was a single function for REAL numbers that does the same thing as CNUM, maybe with a format descriptor like F6.2 thrown in. |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Thu Sep 02, 2010 9:33 pm Post subject: |
|
|
Code: | character*80 str
real x
x = 42.7
write(str, '(f6.2)') x
print*, str
end |
|
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Fri Sep 03, 2010 1:20 pm Post subject: Re: |
|
|
Little-Acorn wrote: | brucebowler wrote: | Look at "internal writes" (and their counterpart internal reads). They should do all that you need to convert either way between numbers and strings |
Seems kind of overhead-intensive. Is it? Or is that just my own mental hangover from the days when, when you wrote something to a file, you were actually writing to a physical 9-track tape? |
Shouldn't be particularly overhead intensive, the machine is converting to a string and copying into memory. No real I/O required at all.
Little-Acorn wrote: | In a perfect world, it would be nice if there was a single function for REAL numbers that does the same thing as CNUM, maybe with a format descriptor like F6.2 thrown in. |
See Paul's example. One reason to use internal reads and writes vs CNUM is that CNUM is specific to the salford/silverfrost compiler, ie non-portable. If you think you might EVER want to move to a different compiler, standard-conforming code is a HUGH help :-) |
|
Back to top |
|
 |
Little-Acorn
Joined: 06 Jul 2008 Posts: 111 Location: San Diego
|
Posted: Fri Sep 03, 2010 6:50 pm Post subject: Re: |
|
|
PaulLaidler wrote: | Code: | character*80 str
real x
x = 42.7
write(str, '(f6.2)') x
print*, str
end |
|
Aw, that's WAY too easy and intuitive. |
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Mon Sep 06, 2010 11:31 am Post subject: |
|
|
Internal writes are great, BUT they always involve a few lines of code, and those lines of code always entail hard-coded data such as a format. So it's always a fag to implement one. Sometimes I think it would be fun to write a couple of routines that would bundle up all the gubbins into a single call once and for all. Then I wonder why no-one's done it yet and think it must not be as easy as it looks? |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Mon Sep 06, 2010 12:35 pm Post subject: Re: |
|
|
sparge wrote: | Internal writes are great, BUT they always involve a few lines of code, and those lines of code always entail hard-coded data such as a format. So it's always a fag to implement one. Sometimes I think it would be fun to write a couple of routines that would bundle up all the gubbins into a single call once and for all. Then I wonder why no-one's done it yet and think it must not be as easy as it looks? |
For the "simple" case, Internal writes add 1 line of code (see Paul's example), for the more complex case, it takes 2 lines (the WRITE and the FORMAT). There is no function that can be written that takes fewer lines than that, and then there's the call overhead to think about... |
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Mon Sep 06, 2010 3:30 pm Post subject: |
|
|
Well, yes ... but the "simple" case is only simple because it hides away, in its midst, a conscious decision on the part of the programmer as to exactly what the format should be. And the more complex case does not eliminate the need for that conscious decision, it just separates it out, which is a good thing in my book. Part of that decision is based on knowing whether the variable is integer or real or whatever, part of it is based on knowing how big the number is, and part of it is based on how precisely it needs to be represented.
The format ... aye, there's the rub. All the hard stuff either needs extra lines of code, or gets precomputed in the programmers head. Wouldn't it be nice to have a function:
char_variable = based_on_internal_write (variable)
(or even a small set of such functions, one for each numerical data type) that figured out all that stuff on its own? |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Mon Sep 06, 2010 5:04 pm Post subject: Re: |
|
|
[quote="sparge"]Well, yes ... but the "simple" case is only simple because it hides away a conscious decision on the part of the programmer as to exactly what the format should be.
char_variable = based_on_internal_write (variable)[/quote]
And who figures "exactly what the format should be" in that case other than "the conscious decision on the part of the programmer"?
In the case of the internal write, it's "in your face" what the format is. In your example, there's NO indication of what it might be (nor is there a mechanism to change it if the original programmer "guessed wrong"). |
|
Back to top |
|
 |
aebolzan
Joined: 06 Jul 2007 Posts: 229 Location: La Plata, Argentina
|
Posted: Mon Sep 06, 2010 6:43 pm Post subject: Re: |
|
|
sparge wrote: | Internal writes are great, BUT they always involve a few lines of code, and those lines of code always entail hard-coded data such as a format. So it's always a fag to implement one. Sometimes I think it would be fun to write a couple of routines that would bundle up all the gubbins into a single call once and for all. Then I wonder why no-one's done it yet and think it must not be as easy as it looks? |
I use the KREAL function of Simpleplot:
character*20 rmax
real*8 rad_max
integer*4 x,y
x=...
y=...
rad_max=3.24264578
call kreal(real(rad_max),rmax)
call draw_characters@(rmax,x,y,red)
The problem is that Simpleplot uses Real*4, so that you need to convert a Real*8 to real*4....but at least for my purposes it works fine...
I also wondered several times why Simpleplot had KREAL and FTN95 did not....but at least the Simpleplot library comes with the FTN95 compiler, so you can use it as a good alternative.....if real*4 is enough for you...
Agustin |
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Tue Sep 07, 2010 2:20 pm Post subject: Re: |
|
|
brucebowler wrote: | And who figures "exactly what the format should be" in that case other than "the conscious decision on the part of the programmer"?
In the case of the internal write, it's "in your face" what the format is. In your example, there's NO indication of what it might be (nor is there a mechanism to change it if the original programmer "guessed wrong"). |
I think you are misunderstanding what I was trying to say. In my (pseudo-code) example, I meant to imply that the function would compute what the format should be.
The only "mechanism" to change an incorrect guess is to recompile with a different guess. Are you saying that a hard-coded format is good because it's in your face, and because you can recompile if you get it wrong? |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Tue Sep 07, 2010 8:12 pm Post subject: Re: |
|
|
sparge wrote: | I think you are misunderstanding what I was trying to say. In my (pseudo-code) example, I meant to imply that the function would compute what the format should be.
The only "mechanism" to change an incorrect guess is to recompile with a different guess. Are you saying that a hard-coded format is good because it's in your face, and because you can recompile if you get it wrong? |
I work in a scientific environment. I, and the folks I work with, want *explicit* control over what gets written. I can do that with format statements. I imagine that folks who work in the "money business" have similar desires. In either case, if the programmer "gets it wrong", we go change the format statement, recompile and off we go.
Probably because of my science background, I can't imagine a case where the user would want some random format specified by something the user has no control over. |
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Wed Sep 08, 2010 10:58 am Post subject: |
|
|
I too work in a scientific environment, and have done for more than 25 years. I would be surprised if anyone making regular use of FORTRAN does not work in a scientific environment. So your "I am here" postcard from the scientific high ground did not have to travel very far.
I am struggling to understand what misperception of yours or mine is getting under your skin so badly and generating such thinly-veiled hostility. I said that the hypothetical function would "compute what the format should be". Can you spot the key word? I even suggested what the first simple step in this computation could be. Somehow you translated this into "some random format specified by something the user has no control over".
With your science background, you ought at least to know what straw man reasoning is, even if you cannot yet recognise your own as such. However, I am surprised that your science background does not help you to see that a function is just as editable and recompilable as a format statement. I am also surprised that you classify a programmer "guessing" a format as scientific. |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Wed Sep 08, 2010 12:59 pm Post subject: |
|
|
I'm sorry you interpreted my comments as "hostile", they certainly weren't intended to be.
Since it's clear that one of us (it could very well be me) isn't communicating well, I'm going to not say any more on this subject... |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|