GPWiki.org
GPWiki.org
It is currently Thu May 23, 2013 9:51 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: 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: ');
  readln(n);

  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;

  readln;

end.[quote][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 08, 2009 11:14 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
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
 Profile  
 
 Post subject:
PostPosted: 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
 Profile  
 
 Post subject:
PostPosted: Sat Aug 08, 2009 11:37 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
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
 Profile  
 
 Post subject:
PostPosted: 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
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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 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

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group