Discussion:
Turbo Pascal question
(too old to reply)
john
2007-12-20 19:09:11 UTC
Permalink
Hi, regarding Turbo Pascal 7.x (or the standard), is there any power
function provided?

Something like

power(10,4) meaning 10 ^4, or something similar.


Thanks in advance.
Richard Heathfield
2007-12-20 19:29:28 UTC
Permalink
Post by john
Hi, regarding Turbo Pascal 7.x (or the standard), is there any power
function provided?
Something like
power(10,4) meaning 10 ^4, or something similar.
Pascal offers EXP and LN. N^P = EXP(P * LN(N)).

Thus, 10^4 = EXP(4 * LN(10)).
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dr J R Stockton
2007-12-21 13:44:12 UTC
Permalink
Post by Richard Heathfield
Post by john
Hi, regarding Turbo Pascal 7.x (or the standard), is there any power
function provided?
Something like
power(10,4) meaning 10 ^4, or something similar.
Pascal offers EXP and LN. N^P = EXP(P * LN(N)).
Thus, 10^4 = EXP(4 * LN(10)).
The original example was an integer example, and you have given a float
method. I'm almost sure that your code will almost always give a float
of exact integer value (if in the exact range 0..2^53 for Double and
more for Extended). That may not be good enough.


For integer powers, one can use a multiplying loop, though that would
get slow for 1.000001^1000000 (which gives about e); in which case one
can be cunning as in (written in Borland, but perhaps generic) -

function ExpNum(const A : Xtype ; const B : longint) : Xtype ;
begin if B=0 then ExpNum := 1.0 else
if Odd(B) then ExpNum := ExpNum(A, B-1) * A
else ExpNum := Sqr(ExpNum(A, B div 2)) ;
end {ExpNum} ;

which would for that do about log2(1000000) = 20 recursions. It could
be rewritten iterative.

My <URL:http://www.merlyn.demon.co.uk/pas-math.htm> refers.

With an integer number to be raised to a power, a multiplying loop can
never take unreasonably long, unless the number is -1 0 +1 or runtime
checks are off.


But the actual answer to the OP is "NO" or "NOT IN TP".
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
john
2007-12-21 23:15:05 UTC
Permalink
Post by Dr J R Stockton
Post by Richard Heathfield
Post by john
Hi, regarding Turbo Pascal 7.x (or the standard), is there any power
function provided?
Something like
power(10,4) meaning 10 ^4, or something similar.
Pascal offers EXP and LN. N^P = EXP(P * LN(N)).
Thus, 10^4 = EXP(4 * LN(10)).
The original example was an integer example, and you have given a float
method. I'm almost sure that your code will almost always give a float
of exact integer value (if in the exact range 0..2^53 for Double and
more for Extended). That may not be good enough.
For integer powers, one can use a multiplying loop, though that would
get slow for 1.000001^1000000 (which gives about e); in which case one
can be cunning as in (written in Borland, but perhaps generic) -
function ExpNum(const A : Xtype ; const B : longint) : Xtype ;
begin if B=0 then ExpNum := 1.0 else
if Odd(B) then ExpNum := ExpNum(A, B-1) * A
else ExpNum := Sqr(ExpNum(A, B div 2)) ;
end {ExpNum} ;
which would for that do about log2(1000000) = 20 recursions. It could
be rewritten iterative.
My <URL:http://www.merlyn.demon.co.uk/pas-math.htm> refers.
With an integer number to be raised to a power, a multiplying loop can
never take unreasonably long, unless the number is -1 0 +1 or runtime
checks are off.
But the actual answer to the OP is "NO" or "NOT IN TP".
Yes, Richard's solution requires real, but I need to use longint.

More precisely, I want to do a program that provides and stores the
reverse of a number. For example:


input: 12345

result: 54321


I am using div and mod to do that (with two longints and an array of
longint), and having not thought any other way I can do the above, I
can't use the elegant N^P= exp( P* ln(N) ); solution.

Is there any way I can do casting in Turbo Pascal 7.1 (like in C or C++)?
john
2007-12-21 23:38:43 UTC
Permalink
Post by john
Yes, Richard's solution requires real, but I need to use longint.
More precisely, I want to do a program that provides and stores the
input: 12345
result: 54321
I am using div and mod to do that (with two longints and an array of
longint), and having not thought any other way I can do the above, I
can't use the elegant N^P= exp( P* ln(N) ); solution.
Is there any way I can do casting in Turbo Pascal 7.1 (like in C or C++)?
I made some longints. reals, and used a real temp. Basically the program
as it currently is:


{ A number e.g. 1923=3291 }
program reverse;
uses crt;
var
i, j, number: longint;
temp_array: array[1..5] of longint;
array_element, digit_amount, result: real;

begin
clrscr;

write('Give the number: ');
readln(number);

i:=0;
while (number div 10)<> 0 do
begin
i:=i+1;
temp_array[i]:= number mod 10;
number:= number div 10;
end;

i:= i+1;
temp_array[i]:= number mod 10;

digit_amount:= i;

result:= 0;

for j:= 1 to i do
begin
array_element:= temp_array[j];
result:= result+ array_element* exp( (digit_amount- j)* ln(10) );
end;

writeln;
writeln('The reversed number is: ', result:0:0);

readln;
end.

----------------------------------------------------------------------------


I would prefer result to be of integral type.
Richard Heathfield
2007-12-22 00:58:29 UTC
Permalink
Post by john
More precisely, I want to do a program that provides and stores the
input: 12345
result: 54321
function reverse(input : integer) : integer;
var
result : integer;
last : integer;
begin
result := 0;
while input > 0 do
begin
last := input mod 10;
result := result * 10;
result := result + last;
input := input div 10
end;
reverse := result
end

And that's it (except that I haven't tested it as such). Not sure why you
need powers.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
john
2007-12-22 11:18:49 UTC
Permalink
Post by Richard Heathfield
Post by john
More precisely, I want to do a program that provides and stores the
input: 12345
result: 54321
function reverse(input : integer) : integer;
var
result : integer;
last : integer;
begin
result := 0;
while input > 0 do
begin
last := input mod 10;
result := result * 10;
result := result + last;
input := input div 10
end;
reverse := result
end
And that's it (except that I haven't tested it as such). Not sure why you
need powers.
I checked this, and it does not work for negatives.

I need the powers because i store the reversed digits in an array, and
then want to store them in a variable.

So the reversed digits of 1234 are 4321 and it can be stored in a
variable in the style:

4* 10^3+ 3* 10^2+ 2*10^1+ 1* 10^0.
Richard Heathfield
2007-12-22 11:43:12 UTC
Permalink
Post by john
Post by Richard Heathfield
And that's it (except that I haven't tested it as such). Not sure why
you need powers.
I checked this, and it does not work for negatives.
Well, what do you want it to do for negatives? Does -1234 become -4321 or
4321- ? If the first, the fix is obvious - record the sign, take the
absolute value, and adjust at the end. If the second, use a string and
reverse that instead (which is a lot easier).
Post by john
I need the powers because i store the reversed digits in an array, and
then want to store them in a variable.
So the reversed digits of 1234 are 4321 and it can be stored in a
4* 10^3+ 3* 10^2+ 2*10^1+ 1* 10^0.
Well, integer powers are easy enough. Just remember that

x^1 = x
x^(a + b) = x^a * x^b

You can use these rules to calculate x^n trivially, by observing that if n
is 1, then you're done, otherwise if n is odd, you can simplify the
problem to x * x^(n-1), otherwise you can simplify to x^(n/2) * x^(n/2).
Combine these rules recursively as shown elsethread (by another
respondent), and you're done.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jochen
2007-12-22 21:42:57 UTC
Permalink
hi john!
Post by john
I need the powers because i store the reversed digits in an array, and
then want to store them in a variable.
do u have to store the digits in a temp array? homework?

if so lets have a look at your own code:

your loop:

for j:= 1 to i do
begin
array_element:= temp_array[j];
result:= result+ array_element* exp( (digit_amount- j)* ln(10) );
end;


why not simply make it like this:

for j := digit_amount downto 1 do
result:= result * 10 + temp_array[j];

or if u want or have to keep the structure of the loop:

var multi : integer;

multi := 1;
for j:= 1 to i-1 do multi := multi * 10;
for j:= 1 to i do
begin
array_element:= temp_array[j];
result:= result+ array_element * multi;
multi := multi div 10
end;


greetz
jo
--
http://radio789.net.ms - Radio 789 - We play it ALL
Radiostream: http://stream789.net.ms
john
2007-12-22 23:33:35 UTC
Permalink
Post by Jochen
do u have to store the digits in a temp array? homework?
It was classwork, and is considered completed. In my original code, I
had provided a power function myself.
Post by Jochen
for j := digit_amount downto 1 do
result:= result * 10 + temp_array[j];
Interesting. However I can't make it work. Consider the code:

{ A number e.g. 1923=3291 }
program reverse;
uses crt;
var
i, j, digit_amount, number: longint;
temp_array: array[1..5] of longint;
array_element, result: real;

begin
clrscr;

write('Give the number: ');
readln(number);

i:=0;
while (number div 10)<> 0 do
begin
i:=i+1;
temp_array[i]:= number mod 10;
number:= number div 10;
end;

i:= i+1;
temp_array[i]:= number mod 10;

digit_amount:= i;

result:= 0;

{for j:= 1 to i do
begin
array_element:= temp_array[j];
result:= result+ array_element* exp( (digit_amount- j)* ln(10) );
end;}


for j := digit_amount downto 1 do
result:= result * 10 + temp_array[j];

writeln;
writeln('The reversed number is: ', result:0:0);

readln;
end.
Jochen
2007-12-23 04:56:25 UTC
Permalink
hi
Post by Jochen
for j := digit_amount downto 1 do
result:= result * 10 + temp_array[j];
ooops. should be:

for j := 1 to digit_amount do ...

and no more real variables needed.

greetz
jo

--
http://radio789.net.ms - Radio 789 - We play it ALL
Radiostream: http://stream789.net.ms
scott moore
2007-12-23 18:33:04 UTC
Permalink
Wouldn't it be quicker and easier to post a .pdf scan
of your original homework assignment?
Post by john
More precisely, I want to do a program that provides and stores the
john
2007-12-23 22:59:51 UTC
Permalink
Post by scott moore
Wouldn't it be quicker and easier to post a .pdf scan
of your original homework assignment?
That's cute. It isn't a homework assignment. It was a class do-yourself
exercise and I did it by defining a power function. I just wanted to
make it more optimal and I posted here. Then I optimised the program by
using the suggested "EXP(P * LN(N))" with reals instead of longints, and
then we reached the elegant

for j := 1 to digit_amount do
result:= result * 10 + temp_array[j];

construct.

Dr J R Stockton
2007-12-22 17:51:06 UTC
Permalink
Post by john
More precisely, I want to do a program that provides and stores the
input: 12345
result: 54321
var S : string ;
Str(N, S) ;

T := 0 ;
for J := 1 to S.length do T := T*10 + Ord(S[J]) - 48 ;

or

T = "" ;
for J := 1 to S.length do T = T + S[J] ;

Untested; adjustment may be needed.
Post by john
Is there any way I can do casting in Turbo Pascal 7.1
No such version. To cast in TP/BP, read the manual.

I have no idea whether that code will work in on-topic Pascal; for TP &
BP you should be using comp.lang.pascal.borland; see below; read FAQ.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
john
2007-12-22 18:52:31 UTC
Permalink
Post by Dr J R Stockton
Post by john
More precisely, I want to do a program that provides and stores the
input: 12345
result: 54321
var S : string ;
Str(N, S) ;
T := 0 ;
for J := 1 to S.length do T := T*10 + Ord(S[J]) - 48 ;
or
T = "" ;
for J := 1 to S.length do T = T + S[J] ;
Untested; adjustment may be needed.
Post by john
Is there any way I can do casting in Turbo Pascal 7.1
No such version. To cast in TP/BP, read the manual.
I have no idea whether that code will work in on-topic Pascal; for TP &
BP you should be using comp.lang.pascal.borland; see below; read FAQ.
I want to use integral variables, not strings. That is, it is a
calculation issue not only a display issue.
Dr J R Stockton
2007-12-23 16:36:53 UTC
Permalink
Post by john
Post by Dr J R Stockton
Post by john
More precisely, I want to do a program that provides and stores the
input: 12345
result: 54321
var S : string ;
Str(N, S) ;
T := 0 ;
for J := 1 to S.length do T := T*10 + Ord(S[J]) - 48 ;
or
T = "" ;
for J := 1 to S.length do T = T + S[J] ;
Untested; adjustment may be needed.
Post by john
Is there any way I can do casting in Turbo Pascal 7.1
No such version. To cast in TP/BP, read the manual.
I have no idea whether that code will work in on-topic Pascal; for TP &
BP you should be using comp.lang.pascal.borland; see below; read FAQ.
I want to use integral variables, not strings. That is, it is a
calculation issue not only a display issue.
Then you should use

var S, T, X : longint ;
begin
T := 54321 ;
S := 0 ;
while T>0 do begin S := S * 10 + T mod 10 ; T := T div 10 end ;
writeln(S) end.


If that's coursework, perhaps you should consider a different career.

XP, FU set
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borland.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
Loading...