custom/modules/Opportunities/logic_hooks.php

<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'calculate_field', 'custom/modules/Opportunities/OpportunityLogicHook.php','OpportunityLogicHook', 'calculate_field');
?>


custom/modules/Opportunities/OpportunityLogicHook.php

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class OpportunityLogicHook {
    function calculate_field(&$focus, $event, $arguments) {
        // andopes comment: for float numbers it is necessary to unformat them to avoid wrong calculation
        require_once('modules/Currencies/Currency.php');

        $field1 = unformat_number($focus->field1);
        $field2 = unformat_number($focus->field2);
        $field3 = unformat_number($focus->field3);
        $field4 = $field1 + $field2 + $field3;
        $focus->field4 = unformat_number($field4);
    }
}
?>