gdb has dprintf, which works a follows:
The following:
$ dprintf fib, "n is %d\n", n
Is a shortcut for:
$ break fib
$ commands
silent
printf "n is %d\n", n
cont
end
But usually I find p more useful than printf. Among other things, it supports structures (and does symbolic printing of constants). In other words, instead of printf "n is %d\n" I want to do p n.
Is there any convenience command for doing this?
Related
I would like to use FM package capabilities for the study of a problem in number theory.
I installed the package, compiled libraries, and ran the two test suites supplied, all without any problems.
I wrote test.f:
use fmzm
TYPE (IM), SAVE :: n
n = 0
WRITE(*,*) n
end
and compiled using
gfortran -c -O3 -Wall test.f
gfortran fmsave.o fm.o fmzm90.o test.o -o test
which returned no error or warning. But got disappointed by discovering the output:
./test
200000
The number 200000 appears to be a memory location for the variable n. I experimented a little, and if I change n to complex type (ZM) it outputs 200000, 199999. Similarly if I declare and initialize two variable instead of one.
If I change TYPE (IM), SAVE :: n to INTEGER n, and compile exactly as above, I get the expected 0 as output.
If I replace the code by
n = 0
do
n=n+1
if (n < 10) WRITE(*,*) n
end do
then the output 200000 repeats 9 times and then stops. So it is the WRITE function which only finds the location and not the value. PRINT does the same.
As Vladimir points out, unless you write out a value of a standard type, it seems you have to first convert to a string using a maneuver such as
str = im_format('i100', n)
str = adjustl(str)
str = trim(str)
print*, str
where the middle two lines can be left out.
I'm trying to implement this algorithm but I keep getting a syntax error on the 12th line but I cannot pinpoint what is causing it. I'm new to ocaml and any help would be greatly appreciated.
"To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
Initially, let p equal 2, the first prime number.
Starting from p, enumerate its multiples by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).
Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3."
let prime(n) =
let arr = Array.create n false in
let set_marks (arr , n , prime ) = Array.set arr (n*prime) true in
for i = 2 to n do
set_marks(arr,i,2) done
let findNextPrimeNumberThatIsNotMarked (arr, prime , index ) =
let nextPrime = Array.get arr index in
let findNextPrimeNumberThatIsNotMarkedHelper (arr, prime, index) =
if nextPrime > prime then nextPrime
else prime in
;;
Adding to Jeffrey's answer,
As I have already answered to you at " What exactly is the syntax error here? ",
What you absolutely need to do right now is to install and use a proper OCaml indentation tool, and auto-indent lines. Unexpected auto-indent results often indicate syntactic mistakes like forgetting ;. Without such tools, it is very hard even for talented OCaml programmers to write OCaml code without syntax errors.
There are bunch of auto indenters for OCaml available:
ocp-indent for Emacs and Vim https://github.com/OCamlPro/ocp-indent
Caml mode and Tuareg mode for Emacs
Vim should have some other indenters but I do not know...
OCaml has an expression let a = b in c. Your code ends with in, but where is c? It looks like maybe you should just remove the in at the end.
Looking more closely I see there are more problems than this, sorry.
A function in OCaml is going to look like this roughly:
let f x =
let a = b in
let c = d in
val
Your definition for prime looks exactly like this, except that it ends at the for loop, i.e., with the keyword done.
The rest of the code forms a second, independent, function definition. It has a form like this:
let f x =
let a = b in
let g x = expr in
The syntactic problem is that you're missing an expression after in.
However, your use of indentation suggests you aren't trying to define two different functions. If this is true, you need to rework your code somewhat.
One thing that may be useful (for imperative style programming) is that you can write expr1; expr2 to evaluate two expressions one after the other.
I am working on getting a raytracing code working and I think I may have isolated the problem. I am new to working with Fortran 77, but would like to gain more experience using this language (even if it is dated). I have some EQUIVALENCE statements in one of the subroutines that may be used to pipe variables into the subroutine (this could be half the problem right here).
The subroutine:
c s/r qparxdp
SUBROUTINE QPARAB PARA001
implicit real*8 (a-h, o-z)
character*8 modx, id
C PLAIN PARABOLIC OR QUASI-PARABOLIC PROFILE PARA002
C W(104) = 0. FOR A PLAIN PARABOLIC PROFILE PARA003
C = 1. FOR A QUASI-PARABOLIC PROFILE PARA004
COMMON /XX/ MODX(2),X,PXPR,PXPTH,PXPPH,PXPT,HMAX PARA005
COMMON R(6) /WW/ ID(10),W0,W(400) PARA006
EQUIVALENCE (EARTHR,W(2)),(F,W(6)),(FC,W(101)),(HM,W(102)), PARA007
1 (YM,W(103)),(QUASI,W(104)),(PERT,W(150)) PARA008
data ipass / 0 /
ENTRY ELECTX PARA010
print*, W(2), W(6), W(101), W(102), W(103), W(104), W(150)
print*, ' Electx W(6), f ', F, EARTHR, FC, HM, YM, QUASI, PERT, ipass
ipass = ipass + 1
if(ipass.gt.10000) ipass = 2
if(ipass.eq.1) return
modx(1) = 'qparab'
HMAX=HM PARA011
x = 0.d0
pxpr = 0.d0
pxpth = 0.d0
pxpph = 0.d0
H=R(1)-EARTHR PARA013
if(f.le.0.d0) print*, ' YM', YM
FCF2=(FC/F)**2 PARA014
CONST=1.d0 PARA015
IF (QUASI.EQ.1.d0) CONST=(EARTHR+HM-YM)/R(1) PARA016
Z=(H-HM)/YM*CONST PARA017
X=dMAX1(0.d0,FCF2*(1.d0-Z*Z)) PARA018
print*, 'X in qparx', X, Z
IF (X.EQ.0.d0) GO TO 50 PARA019
IF (QUASI.EQ.1.d0) CONST=(EARTHR+HM)*(EARTHR+HM-YM)/R(1)**2 PARA020
PXPR=-2.d0*Z*FCF2/YM*CONST PARA021
50 IF (PERT.NE.0.d0) CALL ELECT1 PARA022
RETURN PARA023
END PARA024-
Immediately before the subroutine or entry ELECTX is called I placed some print statements in the RINDEX Subroutine/Entry.
I check a few of the inputs immediately before the call of RINDEX
ENTRY RINDEX
write(*,*), 'Starting Rindex in ahnwfnc', F
if(ray.eq.0.d0.and.ipass.eq.0) print*, ' no magnetic field'
ipass = 1
OM=PIT2*1.d6*F
C2=C*C
K2=KR*KR+KTH*KTH+KPH*KPH
OM2=OM*OM
VR =C/OM*KR
VTH=C/OM*KTH
VPH=C/OM*KPH
write(*,*), OM, C2, K2, OM2, VR, VTH, VPH, F
CALL ELECTX
What I get out of this little section of code is:
fstep,fbeg,fend 1. 7. 8.
fbeg,fstep,f 7. 1. 0.
f 7. 7.
f before Rindex 7.
Starting Rindex in ahnwfnc 7.
43982297.2 8.98755431E+10 1. 1.93444246E+15 0.00640514066 0.00231408417
0.000282636641 7.
0. 0. 0. 0. 0. 0. 0.
Electx W(6),f 0. 0. 0. 0. 0. 0. 0. 1
So this is a longwinded way of asking - what is going on? I expected the variables like f, for example, to be passed into the subroutine QPARAB, so when I print in the subroutine, I'd expect to see F = 7. I am probably fundamentally misunderstanding something simple. As I have mentioned, the fact that I can't seem to get variables like F into the subroutine QPARAB is actually a big issue because the following calculations come out to 0s or NaNs. I would expect it to have some value. So maybe the data isn't getting in somehow? Everything else (at this point) seems to be working, to some degree.
Where this code comes from:
And I am using a small shell script (this could be a total mess):
g77 -c -O3 raytr_dp.for readw_dp.for trace_dp.for reach_dp.for backup_d.for dummy.for \
polcar_d.for printr_d.for rkam_dp.for hamltn_d.for ahwfnc_d.for \
qparxdp.for dipoly_d.for spoints.for ggm_dp.for secnds.for
g77 -o main -O3 raytr_dp.o readw_dp.o trace_dp.o reach_dp.o backup_d.o dummy.o \
polcar_d.o printr_d.o rkam_dp.o hamltn_d.o ahwfnc_d.o \
qparxdp.o dipoly_d.o spoints.o ggm_dp.o secnds.o
The g77 routines I am using were downloaded at: http://hpc.sourceforge.net/ and finally I get the same error using gfortran,
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/local/gfortran/libexec/gcc/x86_64-apple-darwin13/4.9.0/lto-wrapper
Target: x86_64-apple-darwin13
Thread model: posix
gcc version 4.9.0 (GCC)
Subroutine QPARAB takes no arguments, e.g. nothing is passed to it. It loads the following variables from common blocks (variables shared between scope) MODX, X, PXPR, PXPTH, PXPPH, PXPT, HMAX, ID, W0, and W. Additionally it declares local scope variables modx and id and then assigns implicit typing to all undeclared variables (which are local in scope).
Your variable of interest, F is equivalent to writing W(6). This says that implicit variable F (type real*8) must have the same memory location as W(6). F isn't passed into this subroutine, it is a name local to the subroutine that is really a specific array member of W. If you want to pass a value into the subroutine into F, you need to set the variable W(6) prior to calling the subroutine. Note that in order to do this you will need W in scope and thus you will need the /WW/ common block referenced in the subroutine you are calling from.
This is a follow up to my get_command_argument() question.
I'm reading a command line argument (arg) into a Fortran program. Then I want to store the value of arg as an integer. ichar() doesn't do the job.
This seems kind of basic, so clearly I'm doing something wrong. Any hints?
program test_get_command_argument
integer :: i,j
character(len=32) :: arg
i = 0
do
call get_command_argument(i,arg)
if (LEN_TRIM(arg) == 0) EXIT
write (*,*) trim(arg)
i = i + 1
end do
j = ichar(arg)
end program
You want to use the "internal files" capability.
You should have a statement like read(arg,*) j.
This will read the character variable arg as if it were a file
and store the result into j.
This isn't an answer but an extended comment:
That's a bizarre way to loop over the command line arguments. What's wrong with the straightforward and obvious
do i = 1, command_argument_count()
call get_command_argument(i,arg)
! do funky stuff
end do
Thank you all for helping. Below this post I put the corrected version's of both scripts which now produce the equal output.
Hello,
I have written a little brute string generation script in python to generate all possible combinations of an alphabet within a given length. It works quite nice, but for the reason I wan't it to be faster I try to port it to C++.
The problem is that my C++ Code is creating far too much combination for one word.
Heres my example in python:
./test.py
gives me
aaa
aab
aac
aad
aa
aba
....
while ./test (the c++ programm gives me)
aaa
aaa
aaa
aaa
aa
Here I also get all possible combinations, but I get them twice ore more often.
Here is the Code for both programms:
#!/usr/bin/env python
import sys
#Brute String Generator
#Start it with ./brutestringer.py 4 6 "abcdefghijklmnopqrstuvwxyz1234567890" ""
#will produce all strings with length 4 to 6 and chars from a to z and numbers 0 to 9
def rec(w, p, baseString):
for c in "abcd":
if (p<w - 1):
rec(w, p + 1, baseString + "%c" % c)
print baseString
for b in range(3,4):
rec(b, 0, "")
And here the C++ Code
#include <iostream>
using namespace std;
string chars="abcd";
void rec(int w,int b,string p){
unsigned int i;
for(i=0;i<chars.size();i++){
if(b < (w-1)){
rec(w, (b+1), p+chars[i]);
}
cout << p << "\n";
}
}
int main ()
{
int a=3, b=0;
rec (a+1,b, "");
return 0;
}
Does anybody see my fault ? I don't have much experience with C++.
Thanks indeed
Here the corrected version:
C++
#include <iostream>
using namespace std;
string chars="abcd";
void rec(int w,int b,string p){
unsigned int i;
for(i=0;i<chars.size();i++){
if(b < (w)){
rec(w, (b+1), p+chars[i]);
}
}
cout << p << "\n";
}
int main ()
{
rec (3,0, "");
return 0;
}
Python
#!/usr/bin/env python
import sys
def rec(w, b, p):
for c in "abcd":
if (b < w - 1):
rec(w, b + 1, p + "%c" % c)
print p
rec(4, 0, "")
Equal Output:
$ ./test > 1
$ ./test.py 3 3 "abcd" "" > 2
$ diff 1 2
$
I think the Python code is also broken but maybe you don't notice because the print is indented by one space too many (hey, now I've seen a Python program with a one-off error!)
Shouldn't the output only happen in the else case? And the reason why the output happens more often is that you call print/cout 4 times. I suggest to change the code:
def rec(w, p, baseString):
if w == p:
print baseString
else:
for ...
Just out of curiosity, is this fast enough?
import itertools, string
alphabet = string.lowercase + string.digits
for numchars in (3, 4):
for x in itertools.product(alphabet, repeat=numchars):
print ''.join(x)
(And make sure you're redirecting output to a file; scrolling huge amounts of text up the screen can be surprisingly slow).
In rec the string p gets printed in every iteration of the loop:
for(i=0;i<chars.size();i++){
// ...
cout << p << "\n";
}
The Python code you posted seems to do the same, but maybe there is something mixed up with the indentation there? Did you maybe mix tabs and spaces in the Python file, leading to surprising results?
You say...:
./test.py
gives me
aaa
aab
(etc), but that's not true of the code you posted: what you get instead is
aa
aa
aa
aa
a
with four repetitions of the leading aa, etc etc. Of course you do: you have the print baseString statement inside the loop of for c in "abcd":, so necessarily it's executed four times. I imagine you want that print out of the loop -- and similarly for the C++ code, where you've also put the output statement smack inside the loop, so it gets repeated.