Оберон-клуб «ВЄДАsoft»
https://zx.oberon.org/forum/

get a percentage of an integer?
https://zx.oberon.org/forum/viewtopic.php?f=10&t=136
Страница 1 из 1

Автор:  slenkar [ 22 авг 2013, 21:05 ]
Заголовок сообщения:  get a percentage of an integer?

Hi I want to get a percentage of an integer
but I cant multiply an integer by a REAL

Код: "OBERON"
  1.  
  2. firstvalue:INTEGER
  3. percentage:INTEGER
  4. result:INTEGER
  5.  


Код: "OBERON"


we are tring to get 45% of 6


Код: "OBERON"

percentage =45

Код: "OBERON"
  1.  
  2. result:=firstvalue*(percentage/100);
  3.  

incompatible assignment!

Автор:  Zorko [ 23 авг 2013, 08:38 ]
Заголовок сообщения:  Re: get a percentage of an integer?

Just use:
Код: "OBERON"
  1. result:=firstvalue*percentage DIV 100;
First multiplication and second division used here for increase the precision, but make sure that size of type INTEGER is enough (otherwise use LONGINT).

DIV is integer division. If you need to use "traditional" integer division (as in Pascal and C), there is option in SYSTEM_Cfg.h:
Код: "C"
#define SYSTEM_Cfg_DIV_as_in_C

Oberon has more "right", more mathematic division designed by scientists (differences are for negative numbers), you can read the paper "Division and Modulus for Computer Scientists" to know about it more.

What division will be more efficient? I think, now it is DIV_as_in_C (for the current implementation of high-level DIV based on C division). But it may be improved by implementing the "right" Oberon's DIV by a low-level routine written in assembler.

Автор:  slenkar [ 23 авг 2013, 19:22 ]
Заголовок сообщения:  Re: get a percentage of an integer?

thanks

I cant seem to get any sound, have you tried it?
Код: "OBERON"
  1. MODULE hi;
  2. IMPORT B := Basic;
  3. BEGIN (*sdgfk*)
  4. B.Init; B.BORDER(B.Blue);
  5. B.BEEP(100,1);
  6. B.BEEP(1000,2);
  7. B.BEEP(1000,3);
  8.  
  9. B.PAUSE(B.WaitAKey);
  10. B.Quit;
  11.  
  12. END hi.

Автор:  Zorko [ 23 авг 2013, 19:43 ]
Заголовок сообщения:  Re: get a percentage of an integer?

This code works perfectly without any changes (I just have added (*$MAIN) ).

Try to run the code as you provided. Basic.BEEP is the same that ZX Basic's BEEP, but duration is set in milliseconds. If the code not works inside your game - maybe you have used BEEP before initializing module Basic?

P.S. Haven't you update Basic.h & Basic.c after I made fix for BEEP for IM2 mode? (and don't forget to rebuild Basic).

Вложения:
Hi_IM2.tap [369 байт]
Скачиваний: 1296
Hi_DI.tap [327 байт]
Скачиваний: 1398

Автор:  slenkar [ 23 авг 2013, 21:18 ]
Заголовок сообщения:  Re: get a percentage of an integer?

I replaced Basic.c and .h and it works now thanks!

Автор:  Saferoll [ 28 авг 2013, 13:58 ]
Заголовок сообщения:  Re: get a percentage of an integer?

Zorko писал(а):
Just use:
Код: "OBERON"
  1. result:=firstvalue*percentage DIV 100;
Note, DIV divides without rounding, fraction is truncated (discarded).
Example: 79*10%=7.9 in REAL. It is 7 with truncating (formula above), the rounding makes 8.
Thе formula with rounding (to the right) must be
Код: "OBERON"
  1. result:=(firstvalue*percentage+50) DIV 100;

Автор:  Zorko [ 28 авг 2013, 18:07 ]
Заголовок сообщения:  Re: get a percentage of an integer?

You are absolutely right, Saferoll. I forget about it, and it's important for increase the precision more.

Oberon has not a function ROUND like in Pascal/Delphi, that rounds REALs not by truncating the fraction. But Oberon just has ENTIER, and ROUND may be implemented in easy way like:
Код: "OBERON"
  1. a := ROUND(b); (* Pascal *)
  2. a := ENTIER(b + 0.5); (* Oberon *)
The same useful method proposed by Saferoll (adapted for calculating INTEGERs).

Страница 1 из 1 Часовой пояс: UTC + 2 часа
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/