# Board index » delphi » TP - sin and div!!!

## TP - sin and div!!!

Hi,

I have to questions-->

1) I'd like to devide two numbers.
writeln(12/7);
this results in a long term!
by typing.
writeln(trunc(12/7);
i just get out 2!
So what can i do to round it to 2 or 3 decimal places like 1.714
2) As i am from Germany i don't know the correct expressions but i'll try to
explain:
Typing in
writeln(sin(2));
results in printing 0.909...
How can i get the other sin (concerning the angles? on the calculator it
i want sin2 to result in 0.0348...
What can i do???

I hope anybody can help me
sincerely

Manuel Kistner
Manuel.Kist...@asam.baynet.de

## Re:TP - sin and div!!!

##### Quote
Manuel Kistner wrote:

> Hi,

> I have to questions-->

> 1) I'd like to devide two numbers.
>     writeln(12/7);
>     this results in a long term!
>     by typing.
>     writeln(trunc(12/7);
>     i just get out 2!
>     So what can i do to round it to 2 or 3 decimal places like 1.714

a) Try 12.0/7.0, it will work.

b)
Function RoundEuro(n : Extended) : Extended;
Begin
RoundEuro := 0.001*(Int(1000.0*(n+0.0005)));
End;

##### Quote
>    Typing in
>    writeln(sin(2));
>    results in printing 0.909...

Function AngleDegToArc(n : Extended) : Extended;
Begin
AngleDegToArc := pi * n / 180.0;
End;

As you heared: 180 degrees is half a circle, which is PI
as the surrounding length of the half-circle.

German: Umfang = 2 * r * pi

Franz Glaser

## Re:TP - sin and div!!!

In article <77t2qb\$...@anews.asam.baynet.de>,

##### Quote
Manuel Kistner <Manuel.Kist...@asam.baynet.de> wrote:
>Hi,

>I have to questions-->

>1) I'd like to devide two numbers.
>    writeln(12/7);
>    this results in a long term!
>    by typing.
>    writeln(trunc(12/7);
>    i just get out 2!
>    So what can i do to round it to 2 or 3 decimal places like 1.714

You can output Writeln(12/7:1:3) where 1 is the total width of the field
(expanded when needed) and 3 is the number of decimals.

##### Quote
>2) As i am from Germany i don't know the correct expressions but i'll try to
>explain:
>   Typing in
>   writeln(sin(2));
>   results in printing 0.909...
>   How can i get the other sin (concerning the angles? on the calculator it
>   i want sin2 to result in 0.0348...
>   What can i do???

Sin(2*(pi/180))

##### Quote
>I hope anybody can help me
>sincerely

>Manuel Kistner
>Manuel.Kist...@asam.baynet.de

Osmo

## Re:TP - sin and div!!!

##### Quote
Ing. Franz Glaser wrote:
> >     writeln(12/7);
> >     this results in a long term!

> a) Try 12.0/7.0, it will work.

What's the difference?

Bye,
Ingo

## Re:TP - sin and div!!!

##### Quote
"Manuel Kistner" <Manuel.Kist...@asam.baynet.de> wrote:
>I'd like to devide two numbers.
>    writeln(12/7);
>    this results in a long term!
>    by typing.
>    writeln(trunc(12/7);
>    i just get out 2!
>    So what can i do to round it to 2 or 3 decimal places like 1.714

Writeln(12/7:0:3)

In the above statement, the colons indicate format specifiers.

The first specifier (zero (0) in the above) specifies the
minimum width of the output field.  If the specified field width
is too small (as in this case) then the output will be as wide
as necessary to contain the number.

The second specifier (three (3) in the above) specifies the
number of decimal places.

Format specifiers work with write, writeln, and str procedures.

Timo Salmi's FAQ for additional information on these topics and
many other frequently (and not so frequently) asked Turbo Pascal

...red

## Re:TP - sin and div!!!

Manuel Kistner schrieb in Nachricht <77t2qb\$...@anews.asam.baynet.de>...
##### Quote
>Hi,

>I have to questions-->

>1) I'd like to devide two numbers.
>    writeln(12/7);
>    this results in a long term!
>    by typing.
>    writeln(trunc(12/7);
>    i just get out 2!

Can't be. I think you get out 1!!! You get 2 when using round instead of
trunc.

##### Quote
>    So what can i do to round it to 2 or 3 decimal places like 1.714

procedure Round2Nths (x: extended {or real, if you don't have a
coprocessor}; n: longint): extended;
begin
Round2Nths := round (x * n) / n
end;
{Round to tenths: writeln (Round2Nths (x, 10));}

Other solution (works with write, writeln and str only):
writeln (x:MinChars:DigitsAfterPoint); {NOTE: Those are CONSTS}
eg writeln (123.4567 : 10 : 2 ) gives (_ is a symbol for SPACE) '    123.46'
writeln (123.4567 : 0 : 2 ) gives (_ is a symbol for SPACE) '123.46'
(0 or 1 mean as many digits as needed)

## Re:TP - sin and div!!!

##### Quote
Ing. Franz Glaser wrote:

> Manuel Kistner wrote:

> > Hi,

> > I have to questions-->

> > 1) I'd like to devide two numbers.
> >     writeln(12/7);
> >     this results in a long term!
> >     by typing.
> >     writeln(trunc(12/7);
> >     i just get out 2!
> >     So what can i do to round it to 2 or 3 decimal places like 1.714

> a) Try 12.0/7.0, it will work.

This is Pascal, which knows how to divide, not C, which must be
taught so ;-)

No, his problem is not an incorrect result (which wouldn't be
a "long term"), but an unpleasant output format.
The solution is

writeln(12/7:1:3);

##### Quote

> b)
> Function RoundEuro(n : Extended) : Extended;
> Begin
>   RoundEuro := 0.001*(Int(1000.0*(n+0.0005)));
> End;

Your C is showing through again ;-)
Int is no valid function or type in Pascal. But if you use Trunc
instead, this is indeed a correct function (though not for Euro,
which has only 2 decimal places ;-))

However, the following is even easier:

function round3(x: extended): extended;
begin
round3:=round(1000*x)/x
end;

Note however that it will fail for too large numbers to
fit into an integer (as will yours).

Another option is to use the comp type, which is really a
64 bit integer type, but treated as real type by the compiler.
Therefore, the following should work as well:

function round_again(x: extended): extended;
var
tmp: comp;
begin
tmp:=1000*x;
round_again:=x/1000;
end;

It's really time for an obfuscated Turbo Pascal contest ;-)

[...]

## Re:TP - sin and div!!!

On Tue, 26 Jan 1999 14:27:40 +0100, Christopher Eltschka

##### Quote
<celts...@physik.tu-muenchen.de> wrote:
> Ing. Franz Glaser wrote:

> > b)
> > Function RoundEuro(n : Extended) : Extended;
> > Begin
> >   RoundEuro := 0.001*(Int(1000.0*(n+0.0005)));
> > End;
> Your C is showing through again ;-)

Definitely not. Franz thinks that C is an obfucated kind of pidgin
English suitable for illitarate low-level-programmers ;-)

##### Quote
> Int is no valid function or type in Pascal.

'Int' is a system function in Borland Pascal which is the topic of
this NG. It returns the truncated value as an EXTENDED/REAL.

##### Quote
> But if you use Trunc
> instead, this is indeed a correct function (though not for Euro,
> which has only 2 decimal places ;-))

> However, the following is even easier:

> function round3(x: extended): extended;
>  begin
>   round3:=round(1000*x)/x
>  end;

Probably you mean

round3:=round(1000*x)/1000

##### Quote
> Note however that it will fail for too large numbers to
> fit into an integer (as will yours).

Not if you use 'int'.

round3 := int(1000*x+0.5)/1000;

##### Quote
> Another option is to use the comp type, which is really a
> 64 bit integer type, but treated as real type by the compiler.
> Therefore, the following should work as well:

> function round_again(x: extended): extended;
>  var
>   tmp: comp;
>  begin
>   tmp:=1000*x;
>   round_again:=x/1000;
>  end;

Probably you mean

round_again := tmp/1000;

Well. I think that all these tries are futile. There isn't an
operation like rounding a floating point number to 3 places after the
decimal point and then store the result as a floating point number.
The stored result is not equal to the intended result because the
intended result is not representable as a binary floating point
number. It will be represented by the best binary approximation to the
intended result.

Why and when would you ever want to use this function ?

Regards
Horst

## Re:TP - sin and div!!!

##### Quote
Christopher Eltschka wrote:

> Ing. Franz Glaser wrote:

> > Manuel Kistner wrote:

> > > Hi,

> > > I have to questions-->

> > > 1) I'd like to devide two numbers.
> > >     writeln(12/7);
> > >     this results in a long term!
> > >     by typing.
> > >     writeln(trunc(12/7);
> > >     i just get out 2!
> > >     So what can i do to round it to 2 or 3 decimal places like 1.714

> > a) Try 12.0/7.0, it will work.

> This is Pascal, which knows how to divide, not C, which must be
> taught so ;-)

> No, his problem is not an incorrect result (which wouldn't be
> a "long term"), but an unpleasant output format.
> The solution is

> writeln(12/7:1:3);

> > b)
> > Function RoundEuro(n : Extended) : Extended;
> > Begin
> >   RoundEuro := 0.001*(Int(1000.0*(n+0.0005)));
> > End;

> Your C is showing through again ;-)
> Int is no valid function or type in Pascal. But if you use Trunc
> instead, this is indeed a correct function (though not for Euro,
> which has only 2 decimal places ;-))

If you have to convert FRF to DEM (as an example) you must
first convert to Euro, rounded with 3 decimal places, than
back to DEM with 2 decimal places. As the newer laws say.

- Show quoted text -

##### Quote

> However, the following is even easier:

> function round3(x: extended): extended;
>  begin
>   round3:=round(1000*x)/x
>  end;

> Note however that it will fail for too large numbers to
> fit into an integer (as will yours).

> Another option is to use the comp type, which is really a
> 64 bit integer type, but treated as real type by the compiler.
> Therefore, the following should work as well:

> function round_again(x: extended): extended;
>  var
>   tmp: comp;
>  begin
>   tmp:=1000*x;
>   round_again:=x/1000;
>  end;

> It's really time for an obfuscated Turbo Pascal contest ;-)

> [...]

## Re:TP - sin and div!!!

##### Quote
Christopher Eltschka wrote:

> Ing. Franz Glaser wrote:
.... snip
> > Function RoundEuro(n : Extended) : Extended;
> > Begin
> >   RoundEuro := 0.001*(Int(1000.0*(n+0.0005)));
> > End;

> Your C is showing through again ;-)

it was not really perfect, because it rounds negative numbers
in the wrong direction! It is necessary to introduce a sign
variable and Abs(n) the value.

if n < 0 then Sign := -1.0 else Sign := 1.0;

##### Quote
> >   RoundEuro := 0.001*(Int(1000.0*(Abs(n)+0.0005)));
> Int is no valid function or type in Pascal.

True, but in clpb I can use it :-)  And the Int function has,
it is somewhat funny, a REAL result!

##### Quote
> But if you use Trunc
> instead, this is indeed a correct function (though not for Euro,
> which has only 2 decimal places ;-))

Wrong!
If you have to convert FRF to DEM (as an example) you must
first convert to Euro, rounded with 3 decimal places. Then
back to DEM with 2 decimal places. As the new laws say!!!
It is no longer permitted to convert FRF to DEM immediately,
but via the Euro, according to stringent rules.

##### Quote
> Note however that it will fail for too large numbers to
> fit into an integer (as will yours).

As stated above, this is why I used Int. Int is the Pascal
equivalent to the old famous "ENTIRE" function of ALGOL. So
I never hesitate to use it.

BUT: it is not really correct, even not with extended reals
it truncates some values illegally! This is why I use

##### Quote
> >   RoundEuro := 0.001*(Int(1000.0*(Abs(n)+0.000500000000001)));

in my real programs. This was a secret until now  ;-)

Franz Glaser

## Re:TP - sin and div!!!

##### Quote
Horst Kraemer wrote:

> > Your C is showing through again ;-)

> Definitely not. Franz thinks that C is an obfucated kind of pidgin
> English suitable for illitarate low-level-programmers ;-)

Not true. In my office shelter I have a box with Borland C++. But,
agreed, I do not like to use it, and following my latest WinXX
crash I did not re-install it. I will need it soon again for my
driver-writing job.

##### Quote

> > Int is no valid function or type in Pascal.

> 'Int' is a system function in Borland Pascal which is the topic of
> this NG. It returns the truncated value as an EXTENDED/REAL.

It may not be standardized, but it is the equivalent of the ALGOL
codeprocedure ENTIRE.
##### Quote

> Well. I think that all these tries are futile. There isn't an
> operation like rounding a floating point number to 3 places after the
> decimal point and then store the result as a floating point number.
> The stored result is not equal to the intended result because the
> intended result is not representable as a binary floating point
> number. It will be represented by the best binary approximation to the
> intended result.

> Why and when would you ever want to use this function ?

I mentioned it in another response: it is necessary for conversion
between different european currencies now, where the Euro must be
calculated as an intermediate result, rounded to 1/10 of a cent.

Franz Glaser

## Re:TP - sin and div!!!

##### Quote
Ing. Franz Glaser wrote:

> Christopher Eltschka wrote:

[...]

##### Quote
> > Int is no valid function or type in Pascal.
> True, but in clpb I can use it :-)  And the Int function has,
> it is somewhat funny, a REAL result!

I admit I didn't know that function. Maybe because up to now,
I didn't need the behaviour.
The "int" just looked like a typical C typecast.

##### Quote

> > But if you use Trunc
> > instead, this is indeed a correct function (though not for Euro,
> > which has only 2 decimal places ;-))
> Wrong!
> If you have to convert FRF to DEM (as an example) you must
> first convert to Euro, rounded with 3 decimal places. Then
> back to DEM with 2 decimal places. As the new laws say!!!
> It is no longer permitted to convert FRF to DEM immediately,
> but via the Euro, according to stringent rules.

Ok, you didn't write that this is only an intermediate result.
Moreover, I didn't know that those intermediate Euro values
get rounded to three digits (nor that they have to be rounded
at all).
If the result is to be Euro, I'm correct (you don't get
1/10 Cent on your bank account, if you do it in Euro, f.ex.).

[...]

## Re:TP - sin and div!!!

On Mon, 01 Feb 1999 21:05:56 +0100, Christopher Eltschka

##### Quote
<celts...@physik.tu-muenchen.de> wrote:
> > Wrong!
> > If you have to convert FRF to DEM (as an example) you must
> > first convert to Euro, rounded with 3 decimal places. Then
> > back to DEM with 2 decimal places. As the new laws say!!!
> > It is no longer permitted to convert FRF to DEM immediately,
> > but via the Euro, according to stringent rules.

> Ok, you didn't write that this is only an intermediate result.
> Moreover, I didn't know that those intermediate Euro values
> get rounded to three digits (nor that they have to be rounded
> at all).

Then it's better to forget it.

The official regulations say litterally that the intermediate EURO
result shall be rounded to _at_ _least_ three decimals (whatever this
means ;-). This clearly allows that the intermediate result is not
rounded at all.

The obvious exact conversion method is to use integer arithmetic, of
course:

Conversion Fidji Dollars->Euro    1.73456
Conversion Samoa Dollars->Euro    2.34567

Converting 3.56 Dollars Fidji->Samoa  cents to cents:

( 356*173456 + 234567 div 2 ) div 234567

Regards
Horst

## Re:TP - sin and div!!!

##### Quote
Horst Kraemer wrote:

> The obvious exact conversion method is to use integer arithmetic, of
> course.

Well, I prefer integer arithmetic too for currency calculations.
And the spreadsheet runs with extended reals. So there is
always the rounding problem with currency values. The summing
procedure first rounds each single value to 2 digits if it
is of currency format.

The problem is that rounding makes big troubles with particular
values, due to the fact that even the many bits of the extended
reals are not sufficient.

Franz Glaser

## Re:TP - sin and div!!!

In article <36b6a37d.30834...@news.snafu.de>,
horst.krae...@snafu.de (Horst Kraemer) wrote:

##### Quote
> On Mon, 01 Feb 1999 21:05:56 +0100, Christopher Eltschka
> <celts...@physik.tu-muenchen.de> wrote:

> > > Wrong!
> > > If you have to convert FRF to DEM (as an example) you must
> > > first convert to Euro, rounded with 3 decimal places. Then
> > > back to DEM with 2 decimal places. As the new laws say!!!
> > > It is no longer permitted to convert FRF to DEM immediately,
> > > but via the Euro, according to stringent rules.

> > Ok, you didn't write that this is only an intermediate result.
> > Moreover, I didn't know that those intermediate Euro values
> > get rounded to three digits (nor that they have to be rounded
> > at all).

> Then it's better to forget it.

> The official regulations say litterally that the intermediate EURO
> result shall be rounded to _at_ _least_ three decimals (whatever this
> means ;-). This clearly allows that the intermediate result is not
> rounded at all.

> The obvious exact conversion method is to use integer arithmetic, of
> course:

> Conversion Fidji Dollars->Euro    1.73456
> Conversion Samoa Dollars->Euro    2.34567

> Converting 3.56 Dollars Fidji->Samoa  cents to cents:

>    ( 356*173456 + 234567 div 2 ) div 234567

Conversions only need to go via the euro for the 11 eurozone currencies, for
all others you can do a direct conversion!

Robert
--
Robert AH Prins
prin...@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------