Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Turbo Pascal question

5 views
Skip to first unread message

john

unread,
Dec 20, 2007, 2:09:11 PM12/20/07
to
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

unread,
Dec 20, 2007, 2:29:28 PM12/20/07
to
john said:

> 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

unread,
Dec 21, 2007, 8:44:12 AM12/21/07
to
In comp.lang.pascal.ansi-iso message <f7OdnZKR3sN...@bt.com>,
Thu, 20 Dec 2007 19:29:28, Richard Heathfield <r...@see.sig.invalid>
posted:

>john said:
>
>> 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

unread,
Dec 21, 2007, 6:15:05 PM12/21/07
to


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

unread,
Dec 21, 2007, 6:38:43 PM12/21/07
to
john wrote:
>
> 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++)?

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

unread,
Dec 21, 2007, 7:58:29 PM12/21/07
to
john said:

> More precisely, I want to do a program that provides and stores the
> reverse of a number. For example:
>
>
> 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.

john

unread,
Dec 22, 2007, 6:18:49 AM12/22/07
to
Richard Heathfield wrote:
> john said:
>
>> More precisely, I want to do a program that provides and stores the
>> reverse of a number. For example:
>>
>>
>> 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

unread,
Dec 22, 2007, 6:43:12 AM12/22/07
to
john said:

> Richard Heathfield wrote:
>>
>> 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).

> 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.

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.

Dr J R Stockton

unread,
Dec 22, 2007, 12:51:06 PM12/22/07
to
In comp.lang.pascal.ansi-iso message <fkhhdq$kpc$1...@ulysses.noc.ntua.gr>,
Sat, 22 Dec 2007 01:15:05, john <jo...@no.spam> posted:

>
>More precisely, I want to do a program that provides and stores the
>reverse of a number. For example:
>
>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.

>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.

john

unread,
Dec 22, 2007, 1:52:31 PM12/22/07
to
Dr J R Stockton wrote:
> In comp.lang.pascal.ansi-iso message <fkhhdq$kpc$1...@ulysses.noc.ntua.gr>,
> Sat, 22 Dec 2007 01:15:05, john <jo...@no.spam> posted:
>> More precisely, I want to do a program that provides and stores the
>> reverse of a number. For example:
>>
>> 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.
>
>> 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.

Jochen

unread,
Dec 22, 2007, 4:42:57 PM12/22/07
to
hi john!

john typed:

> 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

unread,
Dec 22, 2007, 6:33:35 PM12/22/07
to
Jochen wrote:
>
> 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.


> why not simply make it like this:
>
> 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;

Jochen

unread,
Dec 22, 2007, 11:56:25 PM12/22/07
to
hi

john typed:


>
> 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.

Dr J R Stockton

unread,
Dec 23, 2007, 11:36:53 AM12/23/07
to
In comp.lang.pascal.ansi-iso message <fkjmdf$v23$1...@ulysses.noc.ntua.gr>,
Sat, 22 Dec 2007 20:52:31, john <jo...@no.spam> posted:

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.

scott moore

unread,
Dec 23, 2007, 1:33:04 PM12/23/07
to
Wouldn't it be quicker and easier to post a .pdf scan
of your original homework assignment?

john

unread,
Dec 23, 2007, 5:59:51 PM12/23/07
to
scott moore wrote:
>
> 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.

0 new messages