It is currently Sun Dec 08, 2013 8:51 am

 All times are UTC

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: (Pascal) Simple Problem...Posted: Sat Aug 08, 2009 11:03 pm
 Level 1 Cleric

Joined: Tue Jul 07, 2009 8:17 pm
Posts: 12
So Delphi 7 is giving me problems with this code:

Code:
if (c mod 1 = 0) then

The debugger says: "Operator not applicable to this operand type."

Why is it not applicable (c is a real number)?

I'm writing a program to find integer solutions to the Pythagorean theorem (i.e. 3, 4, 5), so I need some way to find out if c is an integer or not (hence the code above). If c happens to be 5, then there will be no remainder, but if it happens to be the square root of 2, it will have .141... as a remainder, and I'll discard it. Is there some way I can get the above code to work? Another way to find out if a number is an integer or not, perhaps?

I really hope you can help. Thanks a lot!

(Here's the whole code, if anyone's interested)

Code:
program Pythagorean_Integral_Solution_Finder;

{\$APPTYPE CONSOLE}

{Daniel Young
August 8, 2009
Find integral solutions to the Pythagorean theorem
}

uses
SysUtils;

var
a : integer;
b : integer;
c : real;
n : integer;

begin
writeln('***Pythagorean Integral Tester***':50);
write('Enter the biggest number you want a and b to be tested to');
write(' in the formula a^2 + b^2 = c^2: ');

a := 1;
b := 1;
c := 0;
while (b < (n+1)) do
begin
while (a < (n+1)) do
begin
c := sqrt(sqr(a) + sqr(b));
if (c mod 1 = 0) then
begin
writeln(a, ' ', b, ' ', c, ' ');
a := a + 1;
end
else
a := a + 1;
end;
b := b + 1;
a := 1;
end;

end.[quote][/quote]

Top

 Post subject: Posted: Sat Aug 08, 2009 11:14 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
I think mod takes integer arguments. c mod 1 is like dividing by 1 and returning the remainder.

I think the function you want is int(c) which take a real and returns a real.

So you'd use

Code:
if (c = int(c)) then

Hope that helps

To be honest, reals are not good for precise calculations like this. They don't represent numbers perfectly, so they can give spurious results.

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Posted: Sat Aug 08, 2009 11:29 pm
 Level 1 Cleric

Joined: Tue Jul 07, 2009 8:17 pm
Posts: 12
Jasmine wrote:
I think mod takes integer arguments. c mod 1 is like dividing by 1 and returning the remainder.

I think the function you want is int(c) which take a real and returns a real.

So you'd use

Code:
if (c = int(c)) then

Hope that helps

To be honest, reals are not good for precise calculations like this. They don't represent numbers perfectly, so they can give spurious results.

Perfect! Did I mention I love you?

You were absolutely right about using reals for c. It's giving me answers that are 'almost' right. What number type would you recommend using for these kinds of precise calculations?

Top

 Post subject: Posted: Sat Aug 08, 2009 11:37 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
Which number type you use will depend on how far you want to go. Standard integer type is 32-bit which will go upto 2147483647. And if that's supposed to be holding a square number, then you can't take your a and b higher than about 40000-something.

Is that enough?

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Posted: Sat Aug 08, 2009 11:54 pm
 Level 1 Cleric

Joined: Tue Jul 07, 2009 8:17 pm
Posts: 12
Jasmine wrote:
Which number type you use will depend on how far you want to go. Standard integer type is 32-bit which will go upto 2147483647. And if that's supposed to be holding a square number, then you can't take your a and b higher than about 40000-something.

Is that enough?

I just found a website giving me details about the different number types. I seem to have found something that works. Thanks a lot!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News