Board index » delphi » How do you MOD float values?

How do you MOD float values?

I want to do this:

var
  x: double;
begin
  Result := x mod 7;
end;

but "mod" only takes Integer values. What can I use that would take float
values?

-BKN

 

Re:How do you MOD float values?


"Bryce K. Nielsen" <br...@sysonyx.com> schreef in bericht
news:421beb69@newsgroups.borland.com...

Quote
>I want to do this:

> var
>  x: double;
> begin
>  Result := x mod 7;
> end;

> but "mod" only takes Integer values. What can I use that would take float
> values?

> -BKN

You can't.
BTW what is 14.3 mod 3 anyway? if you want 2.3 as anwer, you can do

function RealMod (x, y : extended) : extended;
begin
   Result := x - y * Trunc(x/y);
end;

Alfred.

Re:How do you MOD float values?


Quote
> You can't.
> BTW what is 14.3 mod 3 anyway? if you want 2.3 as anwer, you can do

> function RealMod (x, y : extended) : extended;
> begin
>    Result := x - y * Trunc(x/y);
> end;

> Alfred.

The purpose is not mod-ing decimal values, but whole numbers that are larger
than the max-allowed Integer. I'll use that function, I think it will work.
Thanks.

-BKN

Re:How do you MOD float values?


Why not use an Int64 then?
Glynn
Quote
Bryce K. Nielsen wrote:
> > You can't.
> > BTW what is 14.3 mod 3 anyway? if you want 2.3 as anwer, you can do

> > function RealMod (x, y : extended) : extended;
> > begin
> >    Result := x - y * Trunc(x/y);
> > end;

> > Alfred.

> The purpose is not mod-ing decimal values, but whole numbers that are
> larger than the max-allowed Integer. I'll use that function, I think
> it will work.  Thanks.

> -BKN

Re:How do you MOD float values?


"Bryce K. Nielsen" <br...@sysonyx.com> wrote

Quote
> The purpose is not mod-ing decimal values, but
> whole numbers that are larger than the max-allowed
> Integer.
> >    Result := x - y * Trunc(x/y);
> I'll use that function, I think it will work.

Bryce,  I bet that it will *not* work consistently.  I think that
because of the rounding that has to take place to get the
quotient into the intermediate floating binary point, that
it will not pass through the trunc function as you expect.
Regards, JohnH

Re:How do you MOD float values?


"Alfred ten Hoeve" <Alfred.nos...@nospam.nl> p1e v diskusnm p?spvku
news:421bf3aa$1@newsgroups.borland.com...

Quote
> "Bryce K. Nielsen" <br...@sysonyx.com> schreef in bericht
> news:421beb69@newsgroups.borland.com...
> >I want to do this:

> > var
> >  x: double;
> > begin
> >  Result := x mod 7;
> > end;

> > but "mod" only takes Integer values. What can I use that would take
float
> > values?

> > -BKN

> You can't.
> BTW what is 14.3 mod 3 anyway? if you want 2.3 as anwer, you can do

> function RealMod (x, y : extended) : extended;
> begin
>    Result := x - y * Trunc(x/y);
> end;

> Alfred.

the second argument in the operation IS the integer.
Therefore the better approach I think is

function RealMod (x: double; MaxInteger: Integer) : double;
begin
    Result := (trunc(x) mod MaxInteger) + frac(x)
end;

regards

--
Roman
mail: i...@rksolution.cz
URL: www.rksolution.cz

Re:How do you MOD float values?


This is what I use and it works fine.

rval:= x - int(x/y) * y;

Denis

Quote
Bryce K. Nielsen wrote:
> I want to do this:

> var
>   x: double;
> begin
>   Result := x mod 7;
> end;

> but "mod" only takes Integer values. What can I use that would take float
> values?

> -BKN

Re:How do you MOD float values?


Quote
"Bryce K. Nielsen" wrote:

> > You can't.
> > BTW what is 14.3 mod 3 anyway? if you want 2.3 as anwer, you can do

> > function RealMod (x, y : extended) : extended;
> > begin
> >    Result := x - y * Trunc(x/y);
> > end;

> > Alfred.

> The purpose is not mod-ing decimal values, but whole numbers that are larger
> than the max-allowed Integer. I'll use that function, I think it will work.
> Thanks.

> -BKN

An int64 gives 63 binary bits with one bit for the sign while an
extended real type uses up to 64 binary bits. Unless 64 bits is
absolutely necessary I would use int64 types. Caution, when an integer
is expressed as an extended type and it contains around 52 or 53
binary digits Delphi no longer adds them correctly. The units digit
may or may not be correct in the result.

Cheers Hanford

Re:How do you MOD float values?


"Hanford Carr @lmcinvestments.com>" <"hwcarr<no spam> wrote

Quote
> ... Caution, when an integer is expressed as an extended type
> and it contains around 52 or 53 binary digits Delphi no longer
> adds them correctly.

Hanford,
Can you provide an example of this error?
Regards, JohnH

Re:How do you MOD float values?


Quote
Hanford Carr wrote:
> when an integer
> is expressed as an extended type and it contains around 52 or 53
> binary digits Delphi no longer adds them correctly. The units digit
> may or may not be correct in the result.

Hanford,

Do you mean something like below? If so, addition and subtraction can
be shown to be at odds with what some would consider reality well
before 52 or 53 digits. According to D6 help the extended type has
19-20 significant digits.

program Project1;
{$APPTYPE CONSOLE}
var
  i, j, k: Extended;
begin
  i := 123456789012345678901.0;
  j := i + 1;
  k := j - i;
  Writeln(k);
end.

Cheers.

--
Joe

Re:How do you MOD float values?


Quote
> Why not use an Int64 then?

Um, you know, I'm not sure. Probably because I'm not too bright :P

I switched to Int64 and it worked great. Thanks a bundle :)

-BKN

Re:How do you MOD float values?


"Joseph Donnelly" <j_j_donne...@yahoo.com> wrote

Quote
> var i, j, k: Extended;
> begin
>   i := 123456789012345678901.0;

Joe,  Your "123456789012345678901" value, with its
log to the base 2 of about 66.7 and being odd is out of
the range for exact representation by the extended type.
The max odd number that can be exactly stored is
2^64 -1 or about 1.84E19.

Re:How do you MOD float values?


Quote
John Herbster wrote:
> Joe,  Your "123456789012345678901" value, with its
> log to the base 2 of about 66.7 and being odd is out of
> the range for exact representation by the extended type.
> The max odd number that can be exactly stored is
> 2^64 -1 or about 1.84E19.

John,

Agreed, I didn't catch the 'binary' modifier.

Cheers.

--
Joe

Re:How do you MOD float values?


Quote
John Herbster wrote:

> "Hanford Carr @lmcinvestments.com>" <"hwcarr<no spam> wrote
> > ... Caution, when an integer is expressed as an extended type
> > and it contains around 52 or 53 binary digits Delphi no longer
> > adds them correctly.

> Hanford,
> Can you provide an example of this error?
> Regards, JohnH

Yes. Use 1,801,439,850,948,199, add it to itself 10 times and the
result is 18,014,398,509,481,996, multiply it by 10 and the result is
18,014,398,509,481,992.

var sum,x,y: extended;
    k: integer;
begin
x:=1801439850948199;
sum:=0;
for k:=1 to 10 do
    sum:= sum + x;
y:=10*x;
end;

sum holds 18,014,398,509,481,996
y holds 18,014,398,509,481,992

Using the watch window to follow the least significant digit:

9+9 = 8
9+8 = 7
9+7 = 6
9+6 = 6
9+6 = 6
etc.

Cheers Hanford

Re:How do you MOD float values?


Quote
> John Herbster wrote:
> > Can you provide an example of this error?

Hanford Carr  wrote

Quote
> Yes. Use
>   1,801,439,850,948,199,
> add it to itself 10 times and the result is
>   18,014,398,509,481,996,

Hanford,  Thanks for the reply.   I get
  18,014,398,509,481,990
in both your add 10 times and your multiply by 10 cases.

Try putting
  Set8087CW($1332);
after the "begin" in the DPR file or before your
test code.  

I think that your PC is initializing the FPU control word
to double precision instead of extended precision.
See QualityCentral report #8399.

Regards, JohnH

Go to page: [1] [2]

Other Threads