Calculating exp(-x)
#185
Replies: 2 comments 4 replies
-
Hello! Thanks for using PRBMath.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
I used SD59x18 Code snippet 1
SD59x18 int256oneintoSD59x18= wrap(one);
SDdiv=int256SDSubintoSD59x18.div(int256numRatingsintoSD59x18);
int256 SD59x18SDdivtoint256 =unwrap(SDdiv);
SD59x18SDdivtoint256=SD59x18SDdivtoint256/1000000000000000000;
SD59x18SDdivtoint256=SD59x18SDdivtoint256/1000000000000000000;
SDdiv=wrap(SD59x18SDdivtoint256);
SDus=SDdiv.sqrt();
SDusexp=SDus.exp();
REus=int256oneintoSD59x18.div(SDusexp);
SD59x18 int256SICintoSD59x18= SD59x18.wrap(SIC); I have a problem exactly in this part of the code. I changed the one variable from int256 to SD59x18 with a value of one to calculate exp(x)/1 but it returns a value of zero. I am new to Solidity and one more question to convert int256 to SD59x18 or On the contrary, should I use the wrap() function in your library? Thank you for your guidance. Code snippet 2
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import { SD59x18, sd } from "./prb-math-main/src/SD59x18.sol";
import { unwrap, wrap } from "./prb-math-main/src/sd59x18/Helpers.sol";
contract Reputationcalculation {
struct edgeserver{
bool exists;
bool blocklist;
// uint ID;
//uint reputation;
}
mapping (address => edgeserver) public edgeserver_suppliers;
struct mobileuser{
bool exists;
//uint ID;
}
//mapping(address => mapping(address => edgeserver)) public edgeserverID ;
mapping (address => mobileuser) public mobileusers;
mapping(address => mapping(address =>mapping (uint256=>int256))) public reputation;
mapping(int256 => edgeserver) public listedgeservers;
mapping(int256 =>mobileuser) public listedmobileuser;
mapping(address => mapping(address =>mapping (int256=>SD59x18))) public DRusi;
mapping(address => mapping(address =>mapping (int256=>int256))) public ti;
mapping(address => mapping(int256 => int256)) public Rusi;
address payable private owner;
address[] public mobileuserArray;
address[] public edgeserverArray;
int256[] public Eui;
address[] public DRusimobileuser;
address[] public DRusiedgeserver;
address[] public duplicateuser;
uint256 id=1;//
int256 SDSub=0;
int256 SDSqr=0;
int256 one=1;
int256 SIC=0;
int256 IncrementSIC=0;
int256 numRatings =0;
int256 reputationSum=0 ;
int256 reputationAverage=0;
SD59x18 SDdiv;
SD59x18 SDus;
SD59x18 SDusexp;
SD59x18 REus;
SD59x18 f;
SD59x18 DRus;
int256 DRusinumber=1;
int256 tinumber=1;
int256 T=0;
uint n;
constructor () {
owner =payable( msg.sender);
edgeserverArray.push(0x0000000000000000000000000000000000000000);
mobileuserArray.push(0x0000000000000000000000000000000000000000);
reputation[0x0000000000000000000000000000000000000000][0x0000000000000000000000000000000000000000][0]= 0;
n=0;
}
function DirectReputation( address edgeserver,address mobileuser,int256 Eus )public returns(SD59x18) {
address _edgeserver;
address _mobileuser;
int256 t=0;
address duplicate;
for(uint256 i=1;i<mobileuserArray.length;i++)
{
_edgeserver=edgeserverArray[i];
_mobileuser=mobileuserArray[i];
if(mobileuser==_mobileuser){
Eui.push(reputation[_edgeserver][_mobileuser][i]);
reputationSum += reputation[_edgeserver][_mobileuser][i];
numRatings++;
if(edgeserver==_edgeserver)
{
SIC++;
}
}
}
SIC=SIC+1;
if(numRatings==0){
edgeserverArray.push(edgeserver);
mobileuserArray.push(mobileuser);
reputation[edgeserver][mobileuser][id]= Eus;
id++;
}
if(numRatings!=0){
numRatings=numRatings+1;
reputationSum=reputationSum+Eus;
reputationAverage=reputationSum / numRatings;
for(uint i=0;i<Eui.length;i++)
{
SDSqr=Eui[i]-reputationAverage;
SDSub+=SDSqr**2;
}
SDSqr=Eus-reputationAverage;
SDSub+=SDSqr**2;
edgeserverArray.push(edgeserver);
mobileuserArray.push(mobileuser);
reputation[edgeserver][mobileuser][id]=Eus;
id++;
SD59x18 int256SDSubintoSD59x18= SD59x18.wrap(SDSub);
SD59x18 int256numRatingsintoSD59x18= wrap(numRatings);
SD59x18 int256oneintoSD59x18= wrap(one);
SDdiv=int256SDSubintoSD59x18.div(int256numRatingsintoSD59x18);
int256 SD59x18SDdivtoint256 =unwrap(SDdiv);
SD59x18SDdivtoint256=SD59x18SDdivtoint256/1000000000000000000;
SD59x18SDdivtoint256=SD59x18SDdivtoint256/1000000000000000000;
SDdiv=wrap(SD59x18SDdivtoint256);
SDus=SDdiv.sqrt();
SDusexp=SDus.exp();
REus=int256oneintoSD59x18.div(SDusexp);
SD59x18 int256SICintoSD59x18= SD59x18.wrap(SIC);
IncrementSIC=SIC+1;
SD59x18 int256IncrementSICintoSD59x18=SD59x18.wrap(IncrementSIC);
SD59x18 SICdiv=int256SICintoSD59x18.div(int256IncrementSICintoSD59x18);
SD59x18 f=SICdiv.sqrt();
SD59x18 EusSD59x18=wrap(Eus);
DRus=f.mul(REus.mul(EusSD59x18));
DRusimobileuser.push(mobileuser);
DRusiedgeserver.push(edgeserver);
DRusi[edgeserver][mobileuser][DRusinumber]=DRus;
ti[edgeserver][mobileuser][tinumber]=SIC;
DRusinumber++;
tinumber++;
for(uint j=0;j<mobileuserArray.length;j++)
{
_edgeserver=edgeserverArray[j];
_mobileuser=mobileuserArray[j];
if(edgeserver==_edgeserver){
t++;
duplicateuser.push(mobileuserArray[j]);
}
}
uint duplicatN=0;
uint count=duplicateuser.length;
uint j=0;
address[] memory remove = new address[](count);
for( uint i=0;i<count-1;i++){
if(duplicateuser[i]!=duplicateuser[i+1]){
remove[j++]=duplicateuser[i];
}
}
remove[j++]=duplicateuser[count-1];
for(uint i=0;i<count;i++)
{
if(remove[i]!=0x0000000000000000000000000000000000000000)
{
duplicatN++;
}
}
T=t;
n=duplicatN;
}
return f ;
}
function getid() public view returns(uint256 ) {
return id;
}
function getSDSub() public view returns(int256 ) {
return SDSub;
}
function getSDSqr() public view returns(int256 ) {
return SDSqr;
}
function getSIC() public view returns(int256 ) {
return SIC;
}
function getnumRatings() public view returns(int256 ) {
return numRatings;
}
function getreputationSum() public view returns(int256 ) {
return reputationSum;
}
function getreputationAverage() public view returns(int256 ) {
return reputationAverage;
}
function getSDdiv() public view returns(SD59x18 ) {
return SDdiv;
}
function getSDus() public view returns(SD59x18 ) {
return SDus;
}
function getSDusexp() public view returns(SD59x18 ) {
return SDusexp;
}
function getREus() public view returns(SD59x18 ) {
return REus;
}
function getf() public view returns(SD59x18 ) {
return f;
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I used the PaulRBerg/prb-math library but I can't calculate exp(-x) even exp(x)/1 can't be calculated because it doesn't work with fixed numbers, please guide me. Thanks
Beta Was this translation helpful? Give feedback.
All reactions