Оберон-клуб «ВЄДАsoft»

Твердыня модульных языков
Текущее время: 17 июн 2025, 08:03

Часовой пояс: UTC + 2 часа




Начать новую тему Ответить на тему  [ Сообщений: 7 ] 
Автор Сообщение
 Заголовок сообщения: get a percentage of an integer?
СообщениеДобавлено: 22 авг 2013, 21:05 
Не в сети

Сообщения: 104
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!


Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: get a percentage of an integer?
СообщениеДобавлено: 23 авг 2013, 08:38 
Не в сети
Аватара пользователя

Сообщения: 1019
Откуда: Днепропетровская обл.
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.


Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: get a percentage of an integer?
СообщениеДобавлено: 23 авг 2013, 19:22 
Не в сети

Сообщения: 104
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.


Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: get a percentage of an integer?
СообщениеДобавлено: 23 авг 2013, 19:43 
Не в сети
Аватара пользователя

Сообщения: 1019
Откуда: Днепропетровская обл.
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 байт]
Скачиваний: 1294
Hi_DI.tap [327 байт]
Скачиваний: 1397
Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: get a percentage of an integer?
СообщениеДобавлено: 23 авг 2013, 21:18 
Не в сети

Сообщения: 104
I replaced Basic.c and .h and it works now thanks!


Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: get a percentage of an integer?
СообщениеДобавлено: 28 авг 2013, 13:58 
Не в сети
Администратор
Аватара пользователя

Сообщения: 273
Откуда: Россия
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;


Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: get a percentage of an integer?
СообщениеДобавлено: 28 авг 2013, 18:07 
Не в сети
Аватара пользователя

Сообщения: 1019
Откуда: Днепропетровская обл.
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).


Вернуться к началу
 Профиль  
Ответить с цитатой  
Показать сообщения за:  Поле сортировки  
Начать новую тему Ответить на тему  [ Сообщений: 7 ] 

Часовой пояс: UTC + 2 часа


Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 3


Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения

Найти:
Перейти:  
cron
Создано на основе phpBB® Forum Software © phpBB Group
© VEDAsoft Oberon Club