calculate – Eternal Night https://www.shuijingwanwq.com There is no problem not worth solving, and no technology not worth learning! Sun, 31 May 2026 03:56:18 +0000 en-US hourly 1 https://wordpress.org/?v=7.0 Design a high-precision addition function, which supports the addition of long numbers without losing precision, and ignores negative numbers (please do not use built-in functions such as BCADD) https://www.shuijingwanwq.com/en/2021/12/02/14239/ https://www.shuijingwanwq.com/en/2021/12/02/14239/#respond Thu, 02 Dec 2021 03:48:12 +0000 https://www.shuijingwanwq.com/?p=14239 Post Views: 15

1. In the past few days, an interview question during the computer test, design a high-precision addition function, which supports the addition of long numbers without losing the accuracy, and ignores negative numbers (please do not use BCADD and other built-in functions).

2. Check php  official website, bcadd – the addition calculation of two arbitrary precision numbers. as shown in Figure 1

bcadd — 两个任意精度数字的加法计算

Figure 1

3. The final realization code is as follows. It is expected to continue to improve after there is time in the future. At this stage, the fault tolerance of request parameters is not enough, and negative numbers are not supported.


 9) {
			$result[$i + 1] = 1;
		}
	}

	// 将数组转换为字符串并将其反转
	$result = strrev(implode($result));

	// 计算出小数
	$decimal = str_pad(substr($result, -$decimalMaxLen, $scale), $scale, '0');
	
	// 计算出整数
	$can = (($maxLen - $decimalMaxLen < 1) ? '0' : substr($result, 0, -$decimalMaxLen));

	// 拼接整数与小数
	$result = $can . (($scale > 0) ? '.' . $decimal : '');

	return $result;
}

echo highPrecisionAdd('64474.47265', '24562.45124');
?>


4. 64474.47265 and 24562.45124 are added, and the operation result is equal to: 89036.92389, which is in line with expectations.

 

]]>
https://www.shuijingwanwq.com/en/2021/12/02/14239/feed/ 0
ActiveDataFilter implementation in Yii 2.0, support for filtering parameters for fields that do not exist https://www.shuijingwanwq.com/en/2020/09/09/14718/ https://www.shuijingwanwq.com/en/2020/09/09/14718/#respond Wed, 09 Sep 2020 02:22:57 +0000 https://www.shuijingwanwq.com/?p=14718 Post Views: 13

1. In the topic selection list page, whether it has been extended, 0: no; 1: Yes, this field does not exist in the topic selection table. is calculated by program. $time is the current Beijing time, calculated by the server. as shown in Figure 1

在选题列表页面中,是否已延期,0:否;1:是,此字段在选题表中并不存在。是通过程序计算得出。$time 为当前北京时间,由服务端计算得出。

Figure 1


$result['items'][$i]['is_deferred'] = ($item['status'] != Plan::STATUS_COMPLETED && $time > $item['ended_at']) ? 1 : 0 ;


2. In the latest version of the prototype, you need to support the filtering field: is_deferred. as shown in Figure 2

在最新版本的原型中,需要支持字段:is_deferred 的筛选。

Figure 2

3. Through the rules of the analysis program calculation, the final analysis to obtain the query SQL to be generated. 1599189174 is the current Beijing time, which is calculated by the client.


// 已延期
((`pa_plan`.`status` != '6') AND (`pa_plan`.`ended_at` < '1599189174'))

// 未延期
((`pa_plan`.`status` = '6') OR (`pa_plan`.`ended_at` >= '1599189174'))


4. Added request parameters that have been extended: filter[and][0][and][0][status][neq]=6&filter[and][0][and][1][ended_at][lt]=1599189174. in line with expectations. as shown in Figure 3

已延期的请求参数添加:filter[and][0][and][0][status][neq]=6&amp;filter[and][0][and][1][ended_at][lt]=1599189174。符合预期。

Figure 3


SELECT * FROM `pa_plan` WHERE (`pa_plan`.`is_deleted`=0) AND (((`pa_plan`.`created_at` >= '1573716153') AND (`pa_plan`.`created_at` <= '1599119824')) AND (`pa_plan`.`importance`='3') AND (`pa_plan`.`group_id`='015ce30b116ce86058fa6ab4fea4ac63') AND ((`pa_plan`.`status` != '6') AND (`pa_plan`.`ended_at` < '1599189174'))) ORDER BY `pa_plan`.`id` DESC LIMIT 20


5. Unextended request parameter added: filter[and][0][or][0][status]=6&filter[and][0][or][1][ended_at][gte]=1599189174. in line with expectations. as shown in Figure 4

未延期的请求参数添加:filter[and][0][or][0][status]=6&amp;filter[and][0][or][1][ended_at][gte]=1599189174 。符合预期。

Figure 4


SELECT * FROM `pa_plan` WHERE (`pa_plan`.`is_deleted`=0) AND (((`pa_plan`.`created_at` >= '1573716153') AND (`pa_plan`.`created_at` <= '1599119824')) AND (`pa_plan`.`importance`='3') AND (`pa_plan`.`group_id`='015ce30b116ce86058fa6ab4fea4ac63') AND ((`pa_plan`.`status`='6') OR (`pa_plan`.`ended_at` >= '1599189174'))) ORDER BY `pa_plan`.`id` DESC LIMIT 20


6. Summary: In the ActiveDataFilter implementation in Yii 2.0, if the fields that do not exist need to be filtered, you need to find a way to convert them into the existing fields in the table. In order to use the bottom layer that comes with the framework. Avoid manual conversion of filter parameters.

 

]]>
https://www.shuijingwanwq.com/en/2020/09/09/14718/feed/ 0
The process of adding new rows to the code within a certain time range based on TortoiseGit 2.10 https://www.shuijingwanwq.com/en/2020/07/07/14812/ https://www.shuijingwanwq.com/en/2020/07/07/14812/#respond Tue, 07 Jul 2020 06:22:02 +0000 https://www.shuijingwanwq.com/?p=14812 Display log, as shown in Figure 1 2. Start: 2019/01/01 to: 2019/12/31, as shown in Figure 2 3. Click the Statistics button, the statistics dialog box will pop...]]> Post Views: 12

1. TortoiseGit -> Display log, as shown in Figure 1

TortoiseGit -&gt; 显示日志

Figure 1

2. Start: 2019/01/01 to: 2019/12/31, as shown in Figure 2

起始:2019/01/01 至:2019/12/31

Figure 2

3. Click the Statistics button, the statistics dialog box will pop up, and click the calculation button. as shown in Figure 3

点击 统计 按钮,弹出统计对话框,点击 计算 按钮。

Figure 3

4. The total number of changes that do not include adding/deleted files: 58807 (38411 (+) 20396 (-)). Includes total number of changes to add/remove files: 161078 (128299 (+) 32779 (-)). as shown in Figure 4

不包括添加/删除文件的修改的总行数:58807 (38411 (+) 20396 (-))。包括添加/删除文件的修改的总行数:161078 (128299 (+) 32779 (-))。

Figure 4

5. Directory: /vendor is a third-party package that contains the relevant third-party software packages. The number of rows of this directory needs to be subtracted. Right-click on the directory: /vendor -> TortoiseGit -> Display the log. as shown in Figure 5

目录:/vendor 为包含相关的第三方软件包。需要减去此目录的行数。在目录:/vendor 上右键 -&gt; TortoiseGit -&gt; 显示日志。

Figure 5

6. Start: 2019/01/01 to: 2019/12/31, the result is blank, indicating that no directory is added within this time range: /vendor. There is no need to subtract the number of rows of this directory. as shown in Figure 6

起始:2019/01/01 至:2019/12/31,结果为空白,说明在此时间范围内并未添加目录:/vendor。无需减去此目录的行数。

Figure 6

]]>
https://www.shuijingwanwq.com/en/2020/07/07/14812/feed/ 0
The process of adding new rows to the code within a certain time range based on TortoiseGit 2.10 https://www.shuijingwanwq.com/en/2020/07/07/14838/ https://www.shuijingwanwq.com/en/2020/07/07/14838/#respond Tue, 07 Jul 2020 06:22:02 +0000 https://www.shuijingwanwq.com/?p=14838 Display log, as shown in Figure 1 2. Start: 2019/01/01 to: 2019/12/31, as shown in Figure 2 3. Click the Statistics button, the statistics dialog box will pop...]]> Post Views: 11

1. TortoiseGit -> Display log, as shown in Figure 1

TortoiseGit -&gt; 显示日志

Figure 1

2. Start: 2019/01/01 to: 2019/12/31, as shown in Figure 2

起始:2019/01/01 至:2019/12/31

Figure 2

3. Click the Statistics button, the statistics dialog box will pop up, and click the calculation button. as shown in Figure 3

点击 统计 按钮,弹出统计对话框,点击 计算 按钮。

Figure 3

4. The total number of changes that do not include adding/deleted files: 58807 (38411 (+) 20396 (-)). Includes total number of changes to add/remove files: 161078 (128299 (+) 32779 (-)). as shown in Figure 4

不包括添加/删除文件的修改的总行数:58807 (38411 (+) 20396 (-))。包括添加/删除文件的修改的总行数:161078 (128299 (+) 32779 (-))。

Figure 4

5. Directory: /vendor is a third-party package that contains the relevant third-party software packages. The number of rows of this directory needs to be subtracted. Right-click on the directory: /vendor -> TortoiseGit -> Display the log. as shown in Figure 5

目录:/vendor 为包含相关的第三方软件包。需要减去此目录的行数。在目录:/vendor 上右键 -&gt; TortoiseGit -&gt; 显示日志。

Figure 5

6. Start: 2019/01/01 to: 2019/12/31, the result is blank, indicating that no directory is added within this time range: /vendor. There is no need to subtract the number of rows of this directory. as shown in Figure 6

起始:2019/01/01 至:2019/12/31,结果为空白,说明在此时间范围内并未添加目录:/vendor。无需减去此目录的行数。

Figure 6

]]>
https://www.shuijingwanwq.com/en/2020/07/07/14838/feed/ 0
In PHP 7.4, the difference set of the calculated array is checked with an index to compare whether the request parameters in the body are equal to the arrays in the cache https://www.shuijingwanwq.com/en/2020/06/22/14848/ https://www.shuijingwanwq.com/en/2020/06/22/14848/#respond Mon, 22 Jun 2020 02:41:22 +0000 https://www.shuijingwanwq.com/?p=14848 Post Views: 15

1. In postman postman :http://api.pcs-api.localhost/v1/mobile/rtcs/invite-accept, the data in the body is written in the redis cache in the format of an array, as shown in Figure 1

在 Postman 中 POST:http://api.pcs-api.localhost/v1/mobile/rtcs/invite-accept ,将 Body 中的数据以数组的格式写入 Redis 缓存中

Figure 1

The data in the body will change every time the request is requested, and the implementation logic in the program is as follows:


RTC 邀请接受通话:/mobile/rtcs/invite-accept ( mobile/rtc/invite-accept )

1、请求参数列表
( 1 ) user_name:必填,用户名称
( 2 ) call_mode:必填,通话方式,voice_call:语音通话;video_call:视频通话

2、输入数据验证规则
( 1 ) 必填:user_name、call_mode
( 2 ) 字符串:user_name、call_mode
( 3 ) 范围([voice_call, video_call]):call_mode
( 4 ) 判断房间是否存在,是:响应失败

3、操作数据
( 1 ) 设置RTC 邀请接受通话数据的缓存键
( 2 ) 从缓存中取回RTC 邀请接受通话数据
A、如果不存在,则序列化,将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留
B、如果存在,则反序列化,带索引检查计算数组的差集,如果差集不为空,将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留


3. Check the difference set of the calculated array with an index to compare whether the array is equal. Since the data in the body is rigorously verified, it is possible to ensure that the data in the body will only be less than or equal to the data in the cache. The code is implemented as follows:


        // 设置RTC 邀请接受通话数据的缓存键
        $redisCache = Yii::$app->redisCache;
        $gisRtcInviteAcceptCacheKey = implode(':', ['gis', 'rtc', 'invite', 'accept', $identity->group_id]);

        // 从缓存中取回RTC 邀请接受通话数据
        $gisRtcInviteAcceptCacheData = $redisCache[$gisRtcInviteAcceptCacheKey];

        $requestGisRtcInviteAccept = [
            'user_name' => $user_name,
            'call_mode' => $call_mode,
        ];

        if ($gisRtcInviteAcceptCacheData === false) {
            // 将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留
            $redisCache->set($gisRtcInviteAcceptCacheKey, serialize($requestGisRtcInviteAccept));
        } else {
            $gisRtcInviteAccept = unserialize($gisRtcInviteAcceptCacheData);

            // 带索引检查计算数组的差集
            $result = array_diff_assoc($gisRtcInviteAccept, $requestGisRtcInviteAccept);

            if (!empty($result)) {
                // 将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留
                $redisCache->set($gisRtcInviteAcceptCacheKey, serialize($requestGisRtcInviteAccept));
            }
        }


4. Array_diff_assoc() returns an array that includes all values in array1 but not in any other parameter array. Note that the difference between array_diff() is that the key name is also used for comparison. Print the array in the cache, the request data in the body, and the difference set of the calculated array with index checks. in line with expectations. The overwrite cache data is only reset when the difference set with an index check calculation array is not empty.


$gisRtcInviteAccept
Array
(
    [user_name] => huaqiyun
    [call_mode] => voice_call
)

{
    "user_name": "huaqiyun1",
    "call_mode": "voice_call"
}

array(1) {
  ["user_name"]=>
  string(8) "huaqiyun"
}



$gisRtcInviteAccept
Array
(
    [user_name] => huaqiyun
    [call_mode] => voice_call
)

{
    "user_name": "huaqiyun",
    "call_mode": "video_call"
}

array(1) {
  ["call_mode"]=>
  string(10) "voice_call"
}



$gisRtcInviteAccept
Array
(
    [user_name] => huaqiyun
    [call_mode] => voice_call
)

{
    "user_name": "huaqiyun2",
    "call_mode": "video_call"
}

array(2) {
  ["user_name"]=>
  string(8) "huaqiyun"
  ["call_mode"]=>
  string(10) "voice_call"
}


 

]]>
https://www.shuijingwanwq.com/en/2020/06/22/14848/feed/ 0