SRL Archive - FAQ

Simba can't handle extended like scar?


footballjds

2011-04-19 12:29:21

program new;
type
pairOfDice = record
RollNumber: string;
roll1, value: integer;
end;
var
PairOfDi: array of pairOfDice;
i, x, y, z, AverageValue: integer;
AverageVal: extended;
begin
SetArrayLength(PairOfDi, 10);
y := High(PairOfDi);
for i := 0 to y do
begin
PairOfDi[i].roll1 := RandomRange(1, 6);
z := PairOfDi[i].roll1;
for x := 1 to z do
begin
IncEx(PairOfDi[i].value, RandomRange(1,6));
end;
end;
for x := 0 to y do
begin
IncEx(AverageValue, pairOfDi[x].value);
end;
AverageVal := AverageValue/GetArrayLength(PairOfDi);
Writeln((AverageVal));
end.
works in scar.
works in simba.
scar will give me values like: 9.2
simba will print out ints like: 9

Markus

2011-04-19 12:34:05

AverageVal := extended(AverageValue)/extended(GetArrayLength(PairOfDi));

^ Fix'd, works with both Simba and SCAR then.
Seems to be something weird in Pascalscript, SCAR does bug in this way when dealing with word/byte/longword and such.

footballjds

2011-04-19 12:37:27

thanks Markus. I will use this.
However, does it not seem odd that it doesn't assume you want an extended when you are setting the value of an extended?

Markus

2011-04-19 12:38:49

Yea, as I said, Pascalscript weirdness. It typecasts after the calculation instead of before.
Can probably be fixed, but if there's one codebase really don't want to touch, it's PS's.

Nava2

2011-04-19 14:50:07

Its not a bug.. its doing exactly what it should.

This: AverageVal := AverageValue/GetArrayLength(PairOfDi);
AverageValue and GetArrayLength are INTEGER values. Thus, it does INTEGER division.

Simply do:
AverageVal := AveraveValue * 1.0 / GetArrayLength(PairOfDi);

Or convert *one* to extended by Markus` method.

Coh3n

2011-04-19 16:14:01

From: Nava2
Its not a bug.. its doing exactly what it should.

This: AverageVal := AverageValue/GetArrayLength(PairOfDi);
AverageValue and GetArrayLength are INTEGER values. Thus, it does INTEGER division.

Simply do:
AverageVal := AveraveValue * 1.0 / GetArrayLength(PairOfDi);

Or convert *one* to extended by Markus` method.

Yeah but dividing two integers can result in an extended. I always thought it was Simba that rounded stuff like this.

Nava2

2011-04-19 18:13:58

From: Coh3n
Yeah but dividing two integers can result in an extended. I always thought it was Simba that rounded stuff like this.


No, two integers divided should *never* return a float. This is because a float is of a different type. That just makes no sense to return a float!

What PS *probably* does is does float division then truncates after it finishes. Again, this is proper.

Coh3n

2011-04-19 18:20:22

But 5/2=2.5 not 2 or 3.

Smartzkid

2011-04-19 18:21:50

Only if you're doing slow float division. With integer division, 5/2 = 2, and that's how it should be.

If you need more precision, cast it.

i luffs yeww

2011-04-19 18:26:54

From: Smartzkid
Only if you're doing slow float division. With integer division, 5/2 = 2, and that's how it should be.

If you need more precision, cast it.


Exactly. It's important to not do random type changes, imo. Which is why I dislike dynamically typed languages. Can lead to confusion and and bad practice.

Just keep in mind that an extended isn't an integer and you're golden. ;)

Coh3n

2011-04-19 18:37:39

You learn something new every day. :rolleyes:

noidea

2011-04-19 19:14:25

Why is there even extended.. if float and extended are doing the same thing, scrap extended and keep float to keep similar with most other languages.

Smartzkid

2011-04-19 19:23:08

IIRC a float has less precision than an extended

HarryJames

2011-04-19 19:25:26

Float's can also be affected by CPU!

Smartzkid

2011-04-19 19:37:29

Shouldn't with PascalScript, though.

i luffs yeww

2011-04-19 19:41:15

Pascal doesn't even have a "float" type, does it? Or at least PascalScript.

Oh, is it "double?"

Smartzkid

2011-04-19 19:43:28

Derp. Nope.

i luffs yeww

2011-04-19 19:47:00

Well I see no more precision with extended and double.


var
wat: double;
wat2: extended;

begin
wat := 16.5555555555559;
wat2 := wat;
writeln(wat);
writeln(wat2);
end.

if you add a number it cuts it off. :< But still, dunno what the difference is.

From: some site

For all other processors which support floating point operations, the extended type is a nickname for the type which supports the most precision, this is usually the double type. On processors which do not support co-processor operations (and which have the {$E+} switch), the extended type usually maps to the single type.


Guess there is a difference? Dunno. They behaved the same in the small test I gave them. Really not sure what to do to test any other differences. Gonna check FPC docs.

Type | Double | Extended
Range | 5.0E-324 .. 1.7E308 | 1.9E-4932 .. 1.1E4932
SigFigs | 15-16 | 19-20

I guess they have different ranges, and different significant figures.

noidea

2011-04-19 22:10:35

Cool. I had noidea.